【问题标题】:Is invoke-direct really invoking the methods that are defined in the instantiated object?直接调用真的调用实例化对象中定义的方法吗?
【发布时间】:2013-01-10 06:39:23
【问题描述】:

我有一些测试代码,这是一个继承AbstractClassForInterface1ConcreteClass。 在为ConcreteClass的构造函数生成的smali代码中:

# direct methods
.method public constructor <init>()V
    .locals 0

    .prologue
    .line 4
    invoke-direct {p0}, Lorg/ucomb/AbstractClassForInterface1;-><init>()V 

    return-void
.end method

p0实际上是ConcreteClass类型的实例化对象,因为它来自实例化:

new-instance v0, Lorg/ucomb/ConcreteClass1;

invoke-direct {v0}, Lorg/ucomb/ConcreteClass1;-><init>()V

而且很容易理解,对象v0有方法Lorg/ucomb/ConcreteClass1;-&gt;&lt;init&gt;()V

那么在正文中,为什么要像这样在p0 上使用invoke-direct 来调用超类的构造函数:

invoke-direct {p0}, Lorg/ucomb/AbstractClassForInterface1;-&gt;&lt;init&gt;()V ?

invoke-direct 不应该调用没有虚拟方法解析的方法吗?

【问题讨论】:

    标签: android dalvik smali


    【解决方案1】:

    调用直接方法时,会指定被调用的确切方法。在这种情况下,Lorg/ucomb/AbstractClassForInterface1;-&gt;&lt;init&gt;()V。这不是虚拟表查找 - 它保证是 AbstractClassForInterface1 构造函数。如果它是一个虚拟表查找,它将解析为对 &lt;init&gt;()V 方法的最专门的覆盖,这将是您的 ConcreteClass1 类中的 &lt;init&gt;()V 方法。

    【讨论】:

    • 所以你的意思是,*语法上*从invoke-direct指令中的方法路径,如invoke-direct {p0}, Lorg/ucomb/AbstractClassForInterface1;-&gt;&lt;init&gt;()V,我们会知道被调用对象的类应该是Lorg/ucomb/AbstractClassForInterface1,并且有&lt;init&gt; 方法在类中定义?
    • 我不认为我理解你的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 2011-01-06
    • 1970-01-01
    相关资源
    最近更新 更多