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 cisa Lion:
因为c是狮子:
Lion b = new Lion(); // Creates a Lion
Animal c = (Animal) b; // Refers to the Lion through an Animal variable
Now, cis an Animal-typed reference to a Lionobject. The object is still a Lion, it's just the reference to it is limited to Animalstuff. So when you ask that object what its class is (not what your interfaceto it is in the cvariable / 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 mreference is typed Mapand so you can only use it to access the things the Mapinterface defines, but the objectit's referring to is a HashMapinstance.
该m参考类型Map,所以你只能用它来访问的东西Map接口定义,但对象它指的是一个HashMap实例。
回答by Mena
You are invoking getClass.
您正在调用getClass.
Method invocation resolves at runtime, hence it prints Lionand 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 Bananato an Animalfor 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 aand dstill point to a Dogin the heap and will thus use the instance methods of the Dogclass if they override the methods of the Animalclass.
In other words, ais a Dog, but as long as it is declared as (or typecast to) an Animal, it can only use Animalmethods.
但是两者a和d仍然指向Dog堆中的a ,因此Dog如果它们覆盖类的方法,则将使用类的实例方法Animal。换句话说,ais a Dog,但只要声明为(或类型转换为) an Animal,它就只能使用Animal方法。
回答by mnd
Your object cis a reference to an object, and the object must be of type Animalor any sub class. Just because the object reference is Animaldoesn'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,并且您可以调用该类上所需的所有方法。这是多态的一部分。

