[英]Having a hard time getting my child class to accurately overwrite my parent class
I'm not new to coding, but new to polymorphism/inheritance/etc.我对编码并不陌生,但对多态/继承/等不熟悉。 I'm confused about static/dynamic binding, and specifically what happens when I put different subclasses in an array together.我对静态/动态绑定感到困惑,特别是当我将不同的子类放在一个数组中时会发生什么。 I'm working with some very simple animal classes, and just trying to learn the basics.我正在学习一些非常简单的动物课程,并且只是尝试学习基础知识。
I tried defining them as ligers too, but then when I tried to print the array of animals' sizes, they both had -1 as their size.我也尝试将它们定义为狮虎兽,但是当我尝试打印动物大小的数组时,它们的大小都为 -1。 The way I have it now gives p0 the right size, but not p1.我现在拥有它的方式为 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;
}
When I define the ligers as animals, p1 gets -1 as its size instead of the 121 I want it to have.当我将狮虎定义为动物时,p1 的大小为 -1,而不是我想要的 121。 I'm sure the issue is with me calling it an Animal instead of a liger, but I'm not sure what the correct syntax is to fix it.我确定问题在于我称它为 Animal 而不是 liger,但我不确定修复它的正确语法是什么。 I want them to be able to be in an array with tigers.我希望他们能够与老虎排成一列。
Please, what is fs in your line请问,你的线路中的fs是什么
Animal p1 = fs.new Liger();
By the way, take a look at this code and tell me if it does what you are looking for.顺便说一下,看看这段代码,告诉我它是否符合您的要求。
public static void main(final String[] args) {
Animal[] animals = new Animal[10];
Animal p0 = new Liger();
p0.size = 11;
animals[0] = p0;
Animal p1 = new Liger();
animals[1] = p1;
System.out.println("p0.size "+ p0.size); // call as a superclass instance
System.out.println("real p0.size "+ ((p0 instanceof Liger) ? ((Liger)p0).size : ((Tiger)p0).size) ); // cast to call the effective instance of the subclass
System.out.println("p1.size "+ p1.size); // call as a superclass instance
System.out.println("real p1.size "+ ((p1 instanceof Liger) ? ((Liger)p1).size : ((Tiger)p1).size) ); // cast to call the effective instance of the subclass
}
public abstract class Animal {
private int size;
public Animal() {
size = -1;
}
public Animal(int size) {
this.size = size;
}
public void setSize( int size ) {
this.size = size;
}
public int getSize() {
return size;
}
public static void main(final String[] args) {
Animal[] animals = new Animal[2];
animals[0] = new Liger(20);
animals[1] = new Liger();
for(int i = 0; i < animals.length; ++i) {
System.out.println("Size : " + animals[i].getSize());
}
}
}
public class Tiger extends Animal {
public Tiger() {
super();
}
public Tiger(int size) {
super(size);
}
}
public class Liger extends Animal {
public Liger() {
super();
}
public Liger(int size) {
super(size);
}
}
This is just a basic guideline, just for reference.这只是一个基本指南,仅供参考。 The point here is behaviour can be overridden in subsequent child classes.这里的重点是行为可以在后续的子类中被覆盖。 Java Tutorial Java教程
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.