简体   繁体   English

通过类调用动物类的方法A

[英]Calling methodA of Animal class by casting

I m Beginner in Java, so someone please say how to call methodA() in Animal Through Mammal object.i cant understand why we need Downcasting and Upcasting clearly. 我是Java的初学者,所以有人请说如何在Animal Through Mammal对象中调用methodA()。我不明白为什么我们需要清楚地向下转换和向上转换。

class Animal{
void methodA(){
    System.out.println("I m Animal");
}
}
class Mammal extends Animal{
    void methodA(){
        System.out.println("I m Mammal child class of Animal");
    }
    void methodAB(){
        System.out.println("Two child class Dog and Cat I have");
    }
}

public class UpDownCast {

public static void main(String[] args) {

Animal a = new Animal();
a.methodA();

Mammal m = new Mammal();
m.methodA();


Animal A = new Mammal();
if (A instanceof Mammal){
    A.methodA();
}
Animal A1= new Animal();
Mammal M1 =(Mammal)A1;
M1.methodA();

}
}

Thanks in Advance. 提前致谢。

If your instance is Mammal and if you are calling methodA() (which is overriden in Mammal ), java will call Mammal 's methodA() . 如果您的实例是Mammal并且正在调用methodA() (在Mammal中被覆盖 ),则Java将调用MammalmethodA() This is called Dynamic Polymorphism 这称为动态多态

To call methodA() of Animal from Mammal class, you need to invoke it using super keyword, within the Mammal class, something like below 要从Mammal类调用Animal methodA() ,需要在Mammal类中使用super关键字调用它,如下所示

class Mammal extends Animal{
    void methodA(){
        super.methodA();// --> calling Animal's methodA()
    }
    void methodAB(){
        System.out.println("Two child class Dog and Cat I have");
    }
}

Also, below statement will cause ClassCastException as Animal IS NOT A Mammal (where as a Mammal IS A Animal ) 另外,以下语句将导致ClassCastException因为Animal 不是 Mammal (其中Mammal Animal)

Animal A1= new Animal();
Mammal M1 =(Mammal)A1;
M1.methodA();
how to call methodA() in Animal Through Mammal object.

For that you have to ovveride the methodA in Mammal and if you have a Mammal object with you,you can directly call it.In that case no need of casting. 为此,您必须在Mammal检查methodA ,如果您有Mammal对象,则可以直接调用它。在这种情况下,无需强制转换。

First, you must understand , that by casting you are not actually changing the object itself, you are just labeling it differently. 首先,您必须了解 ,通过投射,您实际上并没有更改对象本身,而只是对其进行了不同的标记。

在此处输入图片说明

You cannot call the method of superclass through the subclass object if you have overriden the method. 如果您已重写该方法,则无法通过子类对象调用超类的方法。 As in your case Mammal is the subclass of Animal, hence when you use Mammal object, JVM will look for the methodA implmentation in the Mammal class first, if exists then it will call the Mammal class method, otherwise Animal class method will be called. 由于您的情况是Mammal是Animal的子类,因此当您使用Mammal对象时,JVM会首先在Mammal类中查找methodA实现,如果存在则将调用Mammal类方法,否则将调用Animal类方法。

The idea is quite logical in terms of inheritence, if child has a behavior inherited from Parent and someone calls that behavior on child, then child method will execute. 就继承而言,这种想法是很合乎逻辑的,如果child具有从Parent继承的行为,并且有人在child上调用了该行为,则将执行child方法。 If child does not have a behavior of its own then it will simply inherit it from Parent and hence Parent's method will be called. 如果child没有自己的行为,则它将简单地从Parent继承它,因此将调用Parent的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 搜索动物方法以获取动物类别 - Search animal method for animal class 将List <Animal>转换为List <Dog> - Casting List<Animal> to List<Dog> 动态转换类并调用适当的方法 - Dynamically Casting class and calling appropriate methods 为什么什么时候<t extends animal>选角名单<list<animal> > 列出<list<t> > 导致编译错误,但转换列表<animal>列出<t>引起警告? </t></animal></list<t></list<animal></t> - Why when <T extends Animal> casting List<List<Animal>> to List<List<T>> causes compilation error, but casting List<Animal> to List<T> causes warning? 将类型强制转换为超类并调用重写的方法是否可以接受? (JAVA) - Is type casting to super class and calling an overriden method acceptable? (Java) 覆盖父类的List类型,以避免在调用方法时进行强制转换 - Overwriting the List type of the parent class to avoid casting when calling methods 在没有排序的情况下找到重量最高的动物()(地图<string, animal> (Where Animal - 类))</string,> - Find animals with the highest weight without sorted() (map<String, Animal> (Where Animal - class)) Java 抽象动物类博士未编译 - 初学者 - Dr. Java Abstract Animal Class not Compiling - Beginner 错误:在类中找不到 main(String[]) 方法:Animal - error: can't find main(String[]) method in class: Animal 在Java动物猜谜游戏中找不到或加载主类 - Could not find or load main class in java animal guessing game
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM