Java - 向上转发和向下转发 [英] Java - Upcasting and Downcasting
问题描述
我知道在stackoverflow中有很多文章/问题描述了Java中的向上转换和向下转换。我知道什么是向上倾斜和向下倾斜。但我的问题并不是特别的。
I Knew there are plenty of articles/questions in stackoverflow describing about upcasting and downcasting in Java. And I knew what is upcasting and downcasting. But my question is not specfic to that.
Upcasting - 从子节点转换为父节点 - 编译器需要注意。不需要演员阵容
向下转换 - 从父母到孩子的转换 - 需要明确演员?
Upcasting - Conversion from child to parent - Compiler takes care. No cast is required
Downcasting - Conversion from parent to child - Explicit cast is required
public class Animal {
public void getAnimalName(){
System.out.println("Parent Animal");
}
}
public class Dog extends Animal{
public void getDogName(){
System.out.println("Dog");
}
}
public static void main(String[] args) {
Dog d = new Dog();
Animal a = d; // Upcasting
a.getAnimalName();
Animal vv = new Dog();
Dog cc = (Dog)vv;//DownCasting
cc.getAnimalName();
cc.getDogName();
如果你查看 Animal
和 Dog
类,每个都有自己的方法,如 getAnimalName()
和 getDogName()
。因此 Dog extends Animal
(is-a relationship),所以我们可以在派生类(Subclass)中使用基类(Super Class)方法
现在考虑主方法中的下面一段代码,
所以我在这里创建一个 Dog
对象w.rt 动物
。所以我只能访问 Animal
属性(方法)
If you look into the Animal
and Dog
class, each are having their own methods like getAnimalName()
and getDogName()
. Hence Dog extends Animal
(is-a relationship), so we can use the base class(Super Class) methods in the derived class(Subclass)
Consider the below piece of code in the Main Method now,
So here I'm creating a Dog
object w.rt Animal
. So I can be able to access only the Animal
properties(Methods)
Dog d = new Dog();
Animal a = d; // Upcasting
a.getAnimalName();<br>
O/P : Parent Animal<br><br>
现在让我们说,我想将基类方法覆盖到派生类中
/ p>
Now Let's say, I would like to override the base class methods into the derived class
public class Dog extends Animal{
@Override
public void getAnimalName(){
System.out.println("Parent Animal overridden here");
}
public void getDogName(){
System.out.println("Dog");
}
}<br>
在主要方法中,
And in Main Method,
Dog d = new Dog();
Animal a = d; // Upcasting
a.getAnimalName();<br>
O/P : Parent Animal overridden here<br><br>
即使我正在创建 Dog
object wrt Animal
,但是这里打印的基类方法在dervied类中被重写。
Even though I'm creating a Dog
object w.r.t Animal
, but here it is printing the base class method which is overridden in the dervied class.
O/P : Parent Animal overridden here<br>
想知道为什么它会像这样。这是因为覆盖
?
Wondering why it behaves like this. Is this becasue of the override
?
请提供您宝贵的意见。
推荐答案
Animal a = d;
此行将使您的Animal对象'a'指向Dog类的实例(Dog d =新狗();)。因此,当您调用该函数时,它将调用类dog中的函数。
this line will make your Animal object 'a' point to the instance of Dog class (Dog d = new Dog();). Therefore when you call the function it will invoke the function in class dog.
您实际创建了类Dog的实例 Dog d = new Dog( );
。然后你正在创建一个类Animal的对象,并使它指向类的实例Dog Animal a = d;
You actually created an instance of class Dog Dog d = new Dog();
. Then you are making an object of class Animal and making it point to the instance of class Dog Animal a = d;
这篇关于Java - 向上转发和向下转发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!