Java多态性使用其超类变量创建子类对象 [英] Java polymorphism creating a subclass object using its superclass variable

查看:193
本文介绍了Java多态性使用其超类变量创建子类对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我是一名学生,正在学习Java。有一个概念,我正在抓紧困难,并希望有人可以为我揭示这一点。我的问题是关于多态性。比方说,我有以下代码。

So I am a student and in the process of learning 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. 据我所知,从那以后变量类型是Animal,它将具有Animal的所有特征。但是,由于创建的对象是Lizard,因此将使用Lizard类中的任何重写方法而不是Animal类中的方法。这是正确的>

  1. From what I understand, since the variable type is Animal, a will have all the characteristics of an 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. Is this correct>

此外,在创建?

时将使用哪些类构造函数

感谢您的帮助。我看起来相当

Thanks for any help. I have looked quite

推荐答案


1.据我所知,由于变量类型是Animal, a将具有动物的所有特征。但是,由于创建的对象
是Lizard,Lizard类中的任何重写方法都将使用
而不是Animal类中的方法。这是正确的>

1.From what I understand, since the variable type is Animal, a will have all the characteristics of an 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. Is this correct>

是的,你是对的。


2.另外,在创建一个类时,将使用哪个类构造函数?

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



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

As,Lizard是Animal的子类,First,Lizards构造函数将被调用,然后从Lizards构造函数中,将会调用 Animal构造函数,因为默认情况下,Lizard构造函数中的第一行将是 super(),除非您调用使用 this()重写Lizard的构造函数。在Animal构造函数中,第一行中将再次调用 super()。假设Animal没有扩展任何类, java.lang.Object的构造函数将被调用为 java.lang.Object 是每个对象的超类。

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(). In Animal constructor there will be another call to super() in the first line. 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.

  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
     }
    }

 }

执行顺序


  1. 将调用蜥蜴构造函数

  2. 除非对重载的构造函数进行this()调用,调用super()即,调用动物no-args构造函数

  3. java.lang.Object的构造函数将使用super()

  4. java.lang.Object的构造函数从动物调用代码w执行

  5. 动物构造函数代码将执行

  6. 蜥蜴构造函数代码将执行

  1. Lizards constructor will be invoked
  2. unless there is a this() call to an overloaded constructor, a call to super() i.e., call's Animals no-args Constructor
  3. java.lang.Object's Constructor will be invoked from animal using super()
  4. java.lang.Object's constructor code will execute
  5. Animals constructor code will execute
  6. Lizards constructor code will execute

这篇关于Java多态性使用其超类变量创建子类对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆