Java 将子级转换为父级
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28479811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java cast child to parent
提问by alpha09
Class Lion extends Animal.
Lion 类扩展了 Animal。
Here is my code:
这是我的代码:
Animal a = new Animal();
Lion b = new Lion();
Animal c = (Animal) b;
Animal[] arr = { a, b, c };
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i].getClass().getName());
arr[i].run();
}
The result is:
结果是:
test2.Animal
Animal Run...
test2.Lion
Lion Run...
test2.Lion
Lion Run...
test2.Animal
动物跑...
test2.Lion
狮子跑...
test2.Lion
狮子跑...
From the example seems that "c" is a "Lion", not an "Animal". Why is that happening?
从这个例子看来,“c”是“狮子”,而不是“动物”。为什么会这样?
回答by T.J. Crowder
From the example seems that "c" is a "Lion", not an "Animal". Why is that happening?
从这个例子看来,“c”是“狮子”,而不是“动物”。为什么会这样?
Because c
isa Lion:
因为c
是狮子:
Lion b = new Lion(); // Creates a Lion
Animal c = (Animal) b; // Refers to the Lion through an Animal variable
Now, c
is an Animal
-typed reference to a Lion
object. The object is still a Lion
, it's just the reference to it is limited to Animal
stuff. So when you ask that object what its class is (not what your interfaceto it is in the c
variable / third entry in your array), it tells you it's a Lion
.
现在,c
是Animal
对Lion
对象的类型化引用。对象仍然是 a Lion
,只是对它的引用仅限于Animal
东西。因此,当您询问该对象的类是什么(而不是您的接口在c
变量/数组中的第三个条目中)时,它会告诉您它是Lion
.
This is exactly like this situation:
这正是这种情况:
Map m = new HashMap();
The m
reference is typed Map
and so you can only use it to access the things the Map
interface defines, but the objectit's referring to is a HashMap
instance.
该m
参考类型Map
,所以你只能用它来访问的东西Map
接口定义,但对象它指的是一个HashMap
实例。
回答by Mena
You are invoking getClass
.
您正在调用getClass
.
Method invocation resolves at runtime, hence it prints Lion
and not Animal
.
方法调用在运行时解析,因此它打印Lion
而不是Animal
。
回答by ling_jan
A cast doesn't make the referenced object change its type, it just restricts itself to the methods of the supertype. You couldn't cast a Banana
to an Animal
for these reasons.
强制转换不会使被引用的对象改变其类型,它只是将自身限制在超类型的方法中。由于这些原因,您不能将 a 转换Banana
为 an Animal
。
The cast in your line
你的阵容中的演员
Animal c = (Animal) b;
happens automatically anyways. You just need to specify your cast when you downcast:
无论如何都会自动发生。您只需要在向下转换时指定您的演员表:
Animal a = new Dog();
Dog d = (Dog) a;
But both a
and d
still point to a Dog
in the heap and will thus use the instance methods of the Dog
class if they override the methods of the Animal
class.
In other words, a
is a Dog
, but as long as it is declared as (or typecast to) an Animal
, it can only use Animal
methods.
但是两者a
和d
仍然指向Dog
堆中的a ,因此Dog
如果它们覆盖类的方法,则将使用类的实例方法Animal
。换句话说,a
is a Dog
,但只要声明为(或类型转换为) an Animal
,它就只能使用Animal
方法。
回答by mnd
Your object c
is a reference to an object, and the object must be of type Animal
or any sub class. Just because the object reference is Animal
doesn't guarantee that the object is an Animal
, it just means that it will behave as an Animal
, and you can call all of the methods on that class that you want. This is part of Polymorphism.
您的对象c
是对对象的引用,并且该对象必须是类型Animal
或任何子类。仅仅因为对象引用 isAnimal
并不能保证该对象是 an Animal
,它只是意味着它将表现为 an Animal
,并且您可以调用该类上所需的所有方法。这是多态的一部分。