子类型多态性和数组
2022-09-04 06:32:58
Computer[] labComputers = new Computer[10];
跟
public class Computer {
...
void toString(){
// print computer specs
}
}
public class Notebook extends Computer{
...
void toString(){
// print computer specs + laptop color
}
}
每个下标变量都可以引用对象或对象,因为它是 的子类。对于方法调用,多态性确保调用正确的方法。labComputers[i]
Computer
Notebook
Notebook
Computer
labComputers[i].toString()
toString
我想知道如果我们这样做会怎么样
Notebook[] labComputers = new Notebook[10];
如果我引用对象和对象,我会得到什么类型或错误Computer
Notebook