【发布时间】:2017-03-11 09:23:11
【问题描述】:
Java: 简单的问题...为什么我不能使用 array[0].childMethod?
注意 myList.method() 有效,但是当存储在数组中时,method() 变得不可用。
感谢任何帮助。
public class Main {
public static void main(String[] args) {
Vehicle[] array = new Vehicle[1];
Car myList = new Car();
System.out.println( myList.myMethod() ); //Output: 1
array[0] = myList;
System.out.println( array[0].myMethod() ); //Doesn't work.
}
}
class Vehicle{
}
class Car extends Vehicle{
public int myMethod(){
return 1;
}
}
【问题讨论】:
-
myMethod() 是否在 Vehicle 类中定义?
-
命名错误;为什么你会称汽车对象为 myList?
标签: java arrays inheritance extends