简体   繁体   English

创建基类对象时的运行时多态性

[英]Runtime polymorphism while creating base class object

Please consider the following code. 请考虑以下代码。

class Base{
  Base() {  
      print();   
  }
  void print() { 
      System.out.println("Base"); 
  }
}

class Child extends Base{
   int i =   4;
   public static void main(String[] args){
      Base base = new Child();
      base.print();
   }
   void print() { 
       System.out.println(i); 
   }
}

The program will print 0,4. 该程序将打印0,4。

What I understand by this is, the method to be executed will be selected depending on the class of the actual object so in this case is Child . 我理解的是,将根据实际对象的类来选择要执行的方法,因此在这种情况下是Child So when Base 's constructor is called print method of Child is called so this will print 0,4. 因此,当调用Base的构造函数时,调用Child print方法,这样就会打印0,4。

Please tell if I understood this correctly? 请告诉我是否理解正确吗? If yes, I have one more question, while Base class constructor is running how come JVM can call Child 's method since Child 's object is not created? 如果是,我还有一个问题,而基类构造函数正在运行JVM如何调用Child的方法,因为没有创建Child的对象?

[...] the method to be executed will be selected depending on the class of the actual object [...]将根据实际对象的类别选择要执行的方法

Yes, your understanding is correct: the method is selected based on the type of the object being created, so the call is sent to Child.print() rather than Base.print() . 是的,您的理解是正确的:根据正在创建的对象的类型选择方法,因此调用将发送到Child.print()而不是Base.print()

while Base class constructor is running how come JVM can call Child 's method since Child 's object is not created? Base类构造函数是如何运行的,因为JVM可以调用Child的方法,因为没有创建Child的对象?

When Base 's constructor is running, Child object is already created . Base的构造函数正在运行时,已经创建了 Child对象。 However, it is not fully initialized . 但是,它尚未完全初始化 That is precisely the reason to avoid making calls to methods that can have overrides from inside a constructor: the language allows it, but programmers should take extreme care when doing it. 这正是避免调用可以在构造函数内部覆盖的方法的原因:语言允许它,但程序员在执行时应该格外小心。

See this Q&A for more information on problems that may happen when base class constructors call overridable methods. 有关基类构造函数调用可覆盖方法时可能发生的问题的更多信息,请参阅此问答

the method to be executed will be selected depending on the class of the actual object 将根据实际对象的类来选择要执行的方法

yes that is correct. 对,那是正确的。

Base base = new Child(); Base base = new Child();

. It doesn't happen the way you think while Base class constructor is running how come JVM can call Child's method since Child's object is not created? while Base class constructor is running how come JVM can call Child's method since Child's object is not created?它不会发生这种情况while Base class constructor is running how come JVM can call Child's method since Child's object is not created? it happens. 它发生了。 Here what happens is, Child instance is created, and while Child instance is being created through default constructor of Child, it first calls the super constructor that is where the 0 get printed 这里发生的是,创建了Child实例,并且当Child实例通过Child的默认构造函数创建时,它首先调用that is where the 0 get printed的超级构造函数

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

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