很难让我的子类准确地覆盖我的父类

我对编码并不陌生,但对多态性/继承等不熟悉。我对静态/动态绑定感到困惑,特别是当我将不同的子类放在一个数组中时会发生什么。我正在研究一些非常简单的动物课程,只是想学习基础知识。


我也尝试将它们定义为狮虎,但是当我尝试打印动物大小数组时,它们的大小都是-1。我现在的方式给了 p0 正确的大小,但不是 p1 。


   public abstract class Animal {

    public int size = -1;   

   }


   public class Tiger extends Animal {


   }


   public class Liger extends Animal {

     public int size = 121;

   }


   public static void main(final String[] args) {


    Animal[] animal = new Animal[10];

    Animal p0 = new Liger();

    p0.size = 11;

    animals[0] = p0;


    Animal p1 = new Liger();

    animals[1] = p1;

  }

当我将狮虎定义为动物时,p1 的大小为 -1,而不是我想要的 121。我确信问题在于我将其称为动物而不是狮虎,但我不确定修复它的正确语法是什么。我希望他们能够与老虎并列。


PIPIONE
浏览 127回答 2
2回答

芜湖不芜

public abstract class Animal {&nbsp; &nbsp; private int size;&nbsp; &nbsp; public Animal() {&nbsp; &nbsp; &nbsp; &nbsp; size = -1;&nbsp; &nbsp; }&nbsp; &nbsp; public Animal(int size) {&nbsp; &nbsp; &nbsp; &nbsp; this.size = size;&nbsp; &nbsp; }&nbsp; &nbsp; public void setSize( int size ) {&nbsp; &nbsp; &nbsp; &nbsp; this.size = size;&nbsp; &nbsp; }&nbsp; &nbsp; public int getSize() {&nbsp; &nbsp; &nbsp; &nbsp; return size;&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(final String[] args) {&nbsp; &nbsp; &nbsp; Animal[] animals&nbsp; = new Animal[2];&nbsp; &nbsp; &nbsp; animals[0] = new Liger(20);&nbsp; &nbsp; &nbsp; animals[1] = new Liger();&nbsp; &nbsp; &nbsp; for(int i = 0; i < animals.length; ++i) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Size : " + animals[i].getSize());&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; }}public class Tiger extends Animal {&nbsp; &nbsp; &nbsp; public Tiger() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;super();&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; public Tiger(int size) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;super(size);&nbsp; &nbsp; &nbsp; }}public class Liger extends Animal {&nbsp; &nbsp; &nbsp; public Liger() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;super();&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; public Liger(int size) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;super(size);&nbsp; &nbsp; &nbsp; }&nbsp;&nbsp;}这只是一个基本准则,仅供参考。这里的要点是可以在后续子类中覆盖行为。Java教程

HUH函数

请问你的行中的fs是什么Animal p1 = fs.new Liger();顺便说一句,看看这段代码并告诉我它是否符合您的要求。public static void main(final String[] args) {&nbsp; &nbsp; Animal[] animals = new Animal[10];&nbsp; &nbsp; Animal p0 = new Liger();&nbsp; &nbsp; p0.size = 11;&nbsp; &nbsp; animals[0] = p0;&nbsp; &nbsp; Animal p1 = new Liger();&nbsp; &nbsp; animals[1] = p1;&nbsp; &nbsp; System.out.println("p0.size "+ p0.size); // call as a superclass instance&nbsp; &nbsp; System.out.println("real p0.size "+ ((p0 instanceof Liger) ? ((Liger)p0).size : ((Tiger)p0).size) ); // cast to call the effective instance of the subclass&nbsp; &nbsp; System.out.println("p1.size "+ p1.size); // call as a superclass instance&nbsp; &nbsp; System.out.println("real p1.size "+ ((p1 instanceof Liger) ? ((Liger)p1).size : ((Tiger)p1).size) ); // cast to call the effective instance of the subclass}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java