简体   繁体   English

为什么不打印“超级”?

[英]Why does this not print “super”?

I have this code: 我有以下代码:

public class Superclass {

    public void print() {
        System.out.println("super");
    }
}

public class Subclass extends Superclass {

    public void print() {
        System.out.println("sub");
    }
}

And then this: 然后这个:

Subclass obj4 = new Subclass();
((Superclass) obj4).print();

I would think that the above print statement prints "super," since it is cast to Superclass, but this is not the case - it prints "sub". 我认为上面的print语句打印为“ super”,因为它被强制转换为Superclass,但事实并非如此-它打印了“ sub”。 Why is this? 为什么是这样?

The upcasting of the type doesn't make any different. 类型的转换没有什么不同。 Which toString() to call is determined by the runtime type of the instance , not the reference. 调用哪个toString()实例的运行时类型而不是引用确定。

Since Subclass overrode Superclass 's toString, invoking any Subclass instance will always resolve to Subclass 's toString. 由于Subclass覆盖了Superclass的toString,因此调用任何Subclass实例将始终解析为Subclass的toString。

Object obj = new Subclass();
obj.toString();  // will still return "sub"

This is because you didn't call super.print(). 这是因为您没有调用super.print()。 By default, subclass's method overrides superclass's one, no matter what casting you make 默认情况下,无论您进行哪种类型的转换,子类的方法都将覆盖超类的方法

public class Subclass extends Superclass { 

   public void print () { 
       super.print();
       System.out.println ("sub"); 
   } 
}

Because you are implicitly overriding the method in your subclass. 因为您隐式地覆盖了子类中的方法。

If you have used C# before, this might be confusing, because Java does not need an override keyword to override methods. 如果您以前使用过C#,这可能会造成混淆,因为Java不需要覆盖关键字来覆盖方法。 Java automatically overrides methods with the same signature. Java自动覆盖具有相同签名的方法。

You might see the @Override annotation on methods, but that's just a safeguard to ensure that you're actually overriding something (it prevents typos and such); 您可能会在方法上看到@Override批注,但这只是确保您实际上在重写某些内容(它可以防止输入错误等)的一种保护措施; it does not actually impact your program itself. 它实际上并不会影响您的程序本身。

Typecasting an object doesn't change the object itself. 类型转换对象不会更改对象本身。 It basically helps you to access the fields / methods which are part of the contract of the class you typecast an object reference to. 它基本上可以帮助您访问字段/方法,这些字段/方法是您键入对象引用的类的协定的一部分。 Let's say your Subclass had this definition: 假设您的子类具有以下定义:

public class Subclass extends Superclass { 

  public void print ( ) { 
    System.out.println ("sub"); 
  } 

  public void subClassMethod()
  {
    //do something
  }
}

And you create an object of type Subclass but hold the reference using a Superclass variable: Superclass obj4 = new Subclass (); 然后,您创建一个Subclass类型的对象,但使用Superclass变量保存了引用: Superclass obj4 = new Subclass ();

Now obj4 gives you access only to the contract for Superclass so you cannot call obj4.subClassMethod() which will give you a compile time error. 现在obj4只允许您访问Superclass的合同,因此您无法调用obj4.subClassMethod(),这将给您带来编译时错误。

In this case you will typecast it to Subclass to get access to the subClassMethod: ((Subclass)obj4).subClassMethod() 在这种情况下,您会将其类型转换为Subclass以获得对subClassMethod的访问权: ((Subclass)obj4).subClassMethod()

If you are overriding a method from a superclass you need to include the @Override notation in your sub class. 如果要从超类覆盖方法,则需要在子类中包括@Override表示法。 Also if you want to use the print() method of the super class,in your subclass's print() method you can use super.print(); 另外,如果要使用超类的print()方法,则可以在子类的print()方法中使用super.print(); im 100% that will work. 即时消息100%会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM