[英]In Java super.getClass() prints “Child” not “Parent” - why is that?
In Java classes and objects, we use "this" keyword to reference to the current object within the class. 在Java类和对象中,我们使用“this”关键字来引用类中的当前对象。 In some sense, I believe "this" actually returns the object of itself.
从某种意义上说,我相信“这个”实际上会回归自身的对象。
Example for this: 示例:
class Lion
{
public void Test()
{
System.out.println(this); //prints itself (A Lion object)
}
}
In the scenario of a superclass and subclass. 在超类和子类的场景中。 I thought that "super" keyword would return the object of the superclass.
我认为“超级”关键字会返回超类的对象。 However it seems that I got it wrong this time:
但是这次我似乎弄错了:
Example: 例:
class Parent
{
public Parent(){
}
}
class Child extends Parent
{
public Child(){
System.out.println(super.getClass()); //returns Child. Why?
}
}
My Quesiton: In the above example, I was expecting the compiler to print out class Parent
, however it prints out class Child
. 我的问题:在上面的例子中,我期望编译器打印出
class Parent
,但它打印出class Child
。 Why is this so? 为什么会这样? What super actually returns?
什么超级实际返回?
A method call using super
just ignores any overrides in the current class. 使用
super
的方法调用只会忽略当前类中的任何覆盖。 For example: 例如:
class Parent {
@Override public String toString() {
return "Parent";
}
}
class Child extends Parent {
@Override public String toString() {
return "Child";
}
public void callToString() {
System.out.println(toString()); // "Child"
System.out.println(super.toString()); // "Parent"
}
}
In the case of a call to getClass()
, that's a method which returns the class it's called on, and can't be overridden - so while I can see why you'd possibly expect it to return Parent.class
, it's still using the same implementation as normal, returning Child
. 在调用
getClass()
的情况下,这是一个返回调用它的类的方法,并且不能被覆盖 - 所以当我看到为什么你可能期望它返回Parent.class
,它仍然在使用与正常相同的实现 ,返回Child
。 (If you actually want the parent class, you should look at the Class
API.) (如果您确实需要父类,则应该查看
Class
API。)
This is often used as part of an override, in fact. 事实上,这经常被用作覆盖的一部分。 For example:
例如:
@Override public void validate() {
// Allow the parent class to validate first...
super.validate();
// ... then perform child-specific validation
if (someChildField == 0) {
throw new SomeValidationException("...");
}
}
The getClass() method returns the class of the object. getClass()方法返回对象的类。 Super & this reference the same object.
Super&this引用相同的对象。 So it is not because you reference your object with super that suddenly the object changes class, it remains an instance of the class that was used to instantiate it, hence the getClass() will always return the same Class whether you call it from this or from super.
所以这不是因为你用super引用你的对象,突然对象改变了类,它仍然是用于实例化它的类的实例,因此getClass()将始终返回相同的Class,无论你是从这个调用它还是从超级。
Super is meant to be used to reference an implementation of a method as defined in a super class, it is typically used in those methods that are being overridden in a sub class to call the original behaviour. Super用于引用超类中定义的方法的实现,它通常用在那些在子类中被重写以调用原始行为的方法中。
The super keyword will call an overridden method in the parent. super关键字将在父级中调用重写方法。 Child did not override that method.
孩子没有覆盖那种方法。 So it will faithfully return the correct class which is Child.
所以它将忠实地返回正确的类,即Child。 You do not get an instance of the Parent class this way.
您没有这样获得Parent类的实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.