简体   繁体   English

Java多态性使用其超类变量创建子类对象

[英]Java polymorphism creating a subclass object using its superclass variable

So I am a student and in the process of learning Java. 所以我是一名学生,正在学习Java。 There is one concept that I am having a difficult time grasping and am hoping that someone could shed some light on this for me. 有一个概念,我很难抓住,我希望有人可以为我揭示这一点。 My question is regarding polymorphism. 我的问题是关于多态性。 Let's say for example I have the following code. 比方说,我有以下代码。

Animal a = new Lizard("Lizzy", 6);  //Lizard extends Animal
  1. From what I understand, since the variable type is Animal, a will have all the characteristics of an Animal. 据我所知,由于变量类型是Animal,因此将具有Animal的所有特征。 But, since the object created is a Lizard, any overridden methods in the Lizard class will be used instead of those in the Animal class. 但是,由于创建的对象是Lizard,因此将使用Lizard类中的任何重写方法而不是Animal类中的方法。 Is this correct> 这是正确的吗?

  2. Also, which classes constructor will be used while creating a? 另外,在创建一个类时将使用哪些类构造函数?

Thanks for any help. 谢谢你的帮助。 I have looked quite 我看起来很好

1.From what I understand, since the variable type is Animal, a will have all the characteristics of an Animal. 1.根据我的理解,由于变量类型是Animal,因此将具有Animal的所有特征。 But, since the object created is a Lizard, any overridden methods in the Lizard class will be used instead of those in the Animal class. 但是,由于创建的对象是Lizard,因此将使用Lizard类中的任何重写方法而不是Animal类中的方法。 Is this correct> 这是正确的吗?

yes, you are Right. 是的,你是对的。

2.Also, which classes constructor will be used while creating a? 2.另外,创建一个类时将使用哪些类构造函数?

          Animal a = new Lizard("Lizzy", 6);  //Lizard extends Animal

As, Lizard is a subclass of Animal, First, Lizards constructor will be invoked, then from Lizards constructor, there will be a call to Animal constructor as the first line in your Lizard constructor would be super() by default unless you call an overloaded constructor of Lizard using this() . 因为,Lizard是Animal的子类,首先,将调用Lizards构造函数,然后从Lizards构造函数中调用Animal构造函数,因为Lizard构造函数中的第一行默认为super() ,除非你调用了重载 Lizard的构造函数使用this() In Animal constructor there will be another call to super() in the first line. 在Animal构造函数中,第一行中将再次调用super() assuming Animal doesn't extend any class, java.lang.Object's constructor will be invoked as java.lang.Object is the super class of every object. 假设Animal没有扩展任何类, java.lang.Object's构造函数将被调用,因为java.lang.Object是每个对象的超类。

  public Object() {

    }
    Class Animal {
     public Animal(){
      //there will be a super call here like super()
    }

    class lizard extends Animal {
    public Lizard(your args) {
       //there will be a super() call here and this call's animal's no-args constructor
     }
    }

 }

The order of execution would be 执行的顺序是

  1. Lizards constructor will be invoked 将调用蜥蜴构造函数
  2. unless there is a this() call to an overloaded constructor, a call to super() ie, call's Animals no-args Constructor 除非有一个this()调用重载的构造函数,调用super()即调用的动物no-args构造函数
  3. java.lang.Object's Constructor will be invoked from animal using super() java.lang.Object的构造函数将使用super()从动物调用
  4. java.lang.Object's constructor code will execute java.lang.Object的构造函数代码将执行
  5. Animals constructor code will execute 动物构造函数代码将执行
  6. Lizards constructor code will execute 蜥蜴构造函数代码将执行
  1. This is correct, even though the reference is of type Animal , all method calls will resolve to the definition in Lizard if present, otherwise the version in the next immediate parent will be called and so on. 这是正确的,即使引用是Animal类型,所有方法调用都将解析为Lizard的定义(如果存在),否则将调用下一个直接父级中的版本,依此类推。

  2. a is just a reference and the actual object is of type Lizard . a只是一个引用,实际的对象是Lizard类型。 So, the constructors in Lizard class will be called. 因此,将调用Lizard类中的构造函数。 They in turn can call the constructors in super classes using super() . 反过来,它们可以使用super()调用超类中的构造函数。

  1. Any overridden methods in the Lizard class will be used instead of those in the Animal class

    Yes, you're right 你是对的

  2. which classes constructor will be used while creating a?

    When you create a subclass, it will implicitly call super class's constructor. 当您创建子类时,它将隐式调用超类的构造函数。 Hence, both super class, which is Animal , and sub class, which is Lizard , will be used. 因此,将使用Super类(即Animal )和子类( Lizard

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

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