简体   繁体   English

java中动态绑定和构造函数链接的混淆

[英]Confused between dynamic binding and constructor chaining in java

According to the book I am following, the following code print:根据我正在阅读的书,打印以下代码:

public class DynamicBindingDemo {
   public static void main(String[] args) {

   m(new graduateStudent());
   m(new Student());
   m(new Person());
   m(new Object());
 }

    public static void m(Object x) {
    System.out.println(x.toString());
   }
}

   class GraduateStudent extends Student {
   }

    class Student extends Person {
       public String toString() {
       return "Student";
     }
 }

  class Person extends Object {
   public String toString() {
     return "Person";
   }
 }

Prints:印刷:

Student学生

Student学生

Person

java.lang.Object@130c19b java.lang.Object@130c19b

Can someone please help me explain why?有人可以帮我解释为什么吗? Here how I traced the code:在这里,我如何跟踪代码:

So m(new graduateStudent()) invokes a polymorphic call to m(Object x), the x.toString() is invoked by GraduateStuent class instead of Object class because GraduateStuent is the actual type of the object.所以 m(new graduateStudent()) 调用了一个对 m(Object x) 的多态调用,x.toString() 是由GraduateStuent 类而不是Object 类调用的,因为GraduateStuent 是对象的实际类型。 Before x.toString() can be invoked in GraduateStudent class, the compiler creates a no arg constructor implicitly in the GS class because there isnt one.在可以在GraduateStudent 类中调用x.toString() 之前,编译器在GS 类中隐式创建了一个无参数构造函数,因为没有。 However, before this constructor can be invoked, student class constructor is invoked first because it is the superclass.但是,在调用此构造函数之前,首先调用学生类构造函数,因为它是超类。 Student constructor is created in the student class(because it doesnt have one), but before that constructor can be invoked Persons constructor is invoked because it is the superclass and so on till we reach the Object superclass and its to string methods should be invoked.... Student 构造函数是在 student 类中创建的(因为它没有),但是在可以调用该构造函数之前,会调用 Person 构造函数,因为它是超类,依此类推,直到我们到达 Object 超类,并且应该调用它的 to string 方法。 ...

I am pretty sure I am wrong on many things(I am a college freshman of 2 months who is a little of the course).我很确定我在很多事情上都错了(我是一个 2 个月大的大学新生,对课程有一点了解)。 So can someone explain what i am getting wrong?那么有人可以解释我的错误吗?

Which method is called doesn't depend on constructor execution.调用哪个方法不依赖于构造函数的执行。 There is a trick however, parent's constructor is executed as a first step in the child's constructor.然而,有一个技巧,父的构造函数作为子构造函数的第一步执行。 It will be added by the compiler unless you add it explicitly.除非您明确添加,否则编译器会添加它。 Eg Student's constructor looks like this:例如 Student 的构造函数看起来像这样:

public Student() {
  super(); //parent initialised before child
}

What you actually do is a method overriding.你实际上做的是一个方法覆盖。

You can think of overriding as of replacing a method in any parent class.您可以将覆盖视为替换任何父类中的方法。 Also to understand this you can think of inheritance chain: Graduate -> Student -> Person -> Object为了理解这一点,你可以想到继承链:Graduate -> Student -> Person -> Object

The first toString method found in the chain will be called.在链中找到的第一个 toString 方法将被调用。 Actually this version is used in prototype inheritance, in java it's implemented differently.实际上这个版本用于原型继承,在java中它的实现方式不同。 But for understanding it should be sufficient.但是对于理解它应该足够了。

So regarding you example:所以关于你的例子:

  • You are not overriding parent method of parent Student class so Student's method is called您没有覆盖父 Student 类的父方法,因此调用 Student 的方法
  • Student overrides Person toString method, Student's method is called Student覆盖Person toString方法,调用Student的方法
  • Person overrides Object's toString method, Person's method is called Person覆盖了Object的toString方法,Person的方法被调用
  • Object's toString is called as it has no Parent class and contains method toString.对象的 toString 被调用,因为它没有父类并且包含方法 toString。

You can read more about method overriding in the Internet :)您可以在 Internet 上阅读有关方法覆盖的更多信息:)

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

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