Java:继承和数组,以及它们的子代
Java: inheritance and arrays, and their children
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;
}
}
我认为,您可以简单地转换数组项:
((Car) array[0]).myMethod()
关于问题 "why?" - 编译器不知道数组中是否有 Car 对象或任何其他 Vehicle 继承者。
原因是 myMethod
仅在 Car
中定义。由于 array
是 Vehicle
类型,因此它不知道 myMethod
。这就是说 - 我将 array
中的所有元素用作 Vehicle
,该元素可能有其他方法或参数,但出于 array
的目的,我专注于 Vehicle
class.
一个小的解决方法是将 array[0]
转换为 Car
,即 ((Car)array[0]).myMethod()
。
array[0] 是超级 class 参考。
mylist 是一个子class 对象。
您正在尝试从 superclass 引用变量调用 subclass 的方法。只有当 sub class 中的方法被覆盖时它才会起作用。
请阅读以下概念..
动态方法调度..
感谢任何帮助。
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() ); // output 1
}
}
class Vehicle{
public int myMethod(){
return 100;
}
}
class Car extends Vehicle{
@Override
public int myMethod(){
return 1;
}
}
注意Vehicle
是你的PARENTClass
Vehicle[] array = new Vehicle[1];//instance of parent
Car
是你继承的CHILDclass
Car myList = new Car();//instance of child
Child can access Parent's methods and its own methods but not Vice versa. Parent can't access child's method unless you implement Dynamic Method Dispatch/Runtime Polymorphism . (This is termed as Polymorphism in biology - one of the OOP pillars)
您的 array
是 parent 的实例,因此它无法访问 child 的 myMethod。但是 myList
是 car 的实例,所以它可以访问方法 myMethod
为此你必须声明为
Car[] array = new Car[1];
否则你可以简单地cast
喜欢
((Car)array[0]).myMethod()
如果您将车辆 class 更改为抽象 class,如下所示:
public abstract class vehicle{
然后像这样在车辆中创建一个抽象方法:
abstract int myMethod() ;
那应该可以很好地工作。
如果您想了解更多信息,YouTube 频道 thenewboston 有一些很棒的 java 教程。
我强烈建议您看一看。他们帮了我大忙。
如果您编译代码,您将得到 编译时错误(不是运行时错误)。这背后的原因是:
array[0] 的类型是 Vehicle 然后编译器认为 array[0] 的类型Vehicle 和 myMethod() 方法不存在于 class Vehicle 中。所以错误出现了。
注意:编译不知道任何运行时信息。
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;
}
}
我认为,您可以简单地转换数组项:
((Car) array[0]).myMethod()
关于问题 "why?" - 编译器不知道数组中是否有 Car 对象或任何其他 Vehicle 继承者。
原因是 myMethod
仅在 Car
中定义。由于 array
是 Vehicle
类型,因此它不知道 myMethod
。这就是说 - 我将 array
中的所有元素用作 Vehicle
,该元素可能有其他方法或参数,但出于 array
的目的,我专注于 Vehicle
class.
一个小的解决方法是将 array[0]
转换为 Car
,即 ((Car)array[0]).myMethod()
。
array[0] 是超级 class 参考。
mylist 是一个子class 对象。
您正在尝试从 superclass 引用变量调用 subclass 的方法。只有当 sub class 中的方法被覆盖时它才会起作用。
请阅读以下概念.. 动态方法调度..
感谢任何帮助。
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() ); // output 1
}
}
class Vehicle{
public int myMethod(){
return 100;
}
}
class Car extends Vehicle{
@Override
public int myMethod(){
return 1;
}
}
注意Vehicle
是你的PARENTClass
Vehicle[] array = new Vehicle[1];//instance of parent
Car
是你继承的CHILDclass
Car myList = new Car();//instance of child
Child can access Parent's methods and its own methods but not Vice versa. Parent can't access child's method unless you implement Dynamic Method Dispatch/Runtime Polymorphism . (This is termed as Polymorphism in biology - one of the OOP pillars)
您的 array
是 parent 的实例,因此它无法访问 child 的 myMethod。但是 myList
是 car 的实例,所以它可以访问方法 myMethod
为此你必须声明为
Car[] array = new Car[1];
否则你可以简单地cast
喜欢
((Car)array[0]).myMethod()
如果您将车辆 class 更改为抽象 class,如下所示:
public abstract class vehicle{
然后像这样在车辆中创建一个抽象方法:
abstract int myMethod() ;
那应该可以很好地工作。
如果您想了解更多信息,YouTube 频道 thenewboston 有一些很棒的 java 教程。
我强烈建议您看一看。他们帮了我大忙。
如果您编译代码,您将得到 编译时错误(不是运行时错误)。这背后的原因是:
array[0] 的类型是 Vehicle 然后编译器认为 array[0] 的类型Vehicle 和 myMethod() 方法不存在于 class Vehicle 中。所以错误出现了。
注意:编译不知道任何运行时信息。