'toString'这里是怎么解决的?
How 'toString' get resolved here?
下面的interface I
隐含了class Object
的public方法作为抽象成员。其中之一是 toString()
interface I{
void test(int i);
//implicitly has abstract members matching every public method of 'class Object'
// one of them is toString()
}
下面class W
继承class Object
public方法,其中之一是toString
,
class W implements I{
public static void main(String[] args){
I w = new W();
w.toString(); //How toString() method is resolved here?
}
}
As both, super interface(I
) and super class(Object
) of class W
有toString()
方法,
javac
如何在编译时解析w.toString()
?我了解到这里使用了invokevirtual
指令。
jvm如何在运行时解析w.toString()
?
interface
不会覆盖方法。
您的 class 也没有覆盖方法 toString()
。您必须重写方法 toString()
才能覆盖它。
所以简单的就叫实现toString()
的superclass Object
.
接口不会覆盖任何 class 的方法。当您使用 toString()
方法时,默认的 toString()
从 Object
class 执行,因为您的 class W
扩展自 Object
.如需更多说明,请覆盖 class W
中的 toString()
方法。
@Override
public String toString() {
return "Yo, baby! M here!";
}
那么输出将是:
Yo, baby! M here!
接口没有任何超级 class 甚至对象 class 也没有,例如
public class Main {
public static void main(String[] argv) throws Exception {
Class cls = java.lang.Cloneable.class;
Class sup = cls.getSuperclass(); // will return null
}
}
因此接口永远不会覆盖对象的方法class。
When an interface has no direct SuperInterface, it will create
abstract public method for all those public methods present in the
Object class.
因为该方法是在接口类型 I
类型的表达式上调用的,所以字节码将包含对应方法的 invokeinterface
指令。
在运行时,调用的方法将被确定
Let C
be the class of objectref
. The actual method to be invoked is
selected by the following lookup procedure:
- If
C
contains a declaration for an instance method with the same name and descriptor as the resolved method, then it is the method to be invoked.
- Otherwise, if
C
has a superclass, a search for a declaration of an instance method with the same name and descriptor as the resolved
method is performed, starting with the direct superclass of C
and
continuing with the direct superclass of that class, and so forth,
until a match is found or no further superclasses exist. If a match is
found, then it is the method to be invoked.
- Otherwise, if there is exactly one maximally-specific method (§5.4.3.3) in the superinterfaces of
C
that matches the resolved
method's name and descriptor and is not abstract, then it is the
method to be invoked.
在您的例子中,objectref
是对 W
类型对象的引用。 W
不包含与已解析方法具有相同名称和描述符的实例方法的声明。因此,我们检查 W
、Object
的超类。 Object
有没有有这样的方法。因此调用该方法。
下面的interface I
隐含了class Object
的public方法作为抽象成员。其中之一是 toString()
interface I{
void test(int i);
//implicitly has abstract members matching every public method of 'class Object'
// one of them is toString()
}
下面class W
继承class Object
public方法,其中之一是toString
,
class W implements I{
public static void main(String[] args){
I w = new W();
w.toString(); //How toString() method is resolved here?
}
}
As both, super interface(I
) and super class(Object
) of class W
有toString()
方法,
javac
如何在编译时解析w.toString()
?我了解到这里使用了invokevirtual
指令。
jvm如何在运行时解析w.toString()
?
interface
不会覆盖方法。
您的 class 也没有覆盖方法 toString()
。您必须重写方法 toString()
才能覆盖它。
所以简单的就叫实现toString()
的superclass Object
.
接口不会覆盖任何 class 的方法。当您使用 toString()
方法时,默认的 toString()
从 Object
class 执行,因为您的 class W
扩展自 Object
.如需更多说明,请覆盖 class W
中的 toString()
方法。
@Override
public String toString() {
return "Yo, baby! M here!";
}
那么输出将是:
Yo, baby! M here!
接口没有任何超级 class 甚至对象 class 也没有,例如
public class Main {
public static void main(String[] argv) throws Exception {
Class cls = java.lang.Cloneable.class;
Class sup = cls.getSuperclass(); // will return null
}
}
因此接口永远不会覆盖对象的方法class。
When an interface has no direct SuperInterface, it will create abstract public method for all those public methods present in the Object class.
因为该方法是在接口类型 I
类型的表达式上调用的,所以字节码将包含对应方法的 invokeinterface
指令。
在运行时,调用的方法将被确定
Let
C
be the class ofobjectref
. The actual method to be invoked is selected by the following lookup procedure:
- If
C
contains a declaration for an instance method with the same name and descriptor as the resolved method, then it is the method to be invoked.- Otherwise, if
C
has a superclass, a search for a declaration of an instance method with the same name and descriptor as the resolved method is performed, starting with the direct superclass ofC
and continuing with the direct superclass of that class, and so forth, until a match is found or no further superclasses exist. If a match is found, then it is the method to be invoked.- Otherwise, if there is exactly one maximally-specific method (§5.4.3.3) in the superinterfaces of
C
that matches the resolved method's name and descriptor and is not abstract, then it is the method to be invoked.
在您的例子中,objectref
是对 W
类型对象的引用。 W
不包含与已解析方法具有相同名称和描述符的实例方法的声明。因此,我们检查 W
、Object
的超类。 Object
有没有有这样的方法。因此调用该方法。