多态的经典问题

Java多态有关的一个经典问题

public class hello {
     public static void main(String[] args) {
            A a1 = new A();
            A a2 = new B();//自动向上转型,向下转型要强制转换
            B b = new B();
            C c = new C();
            D d = new D();

            System.out.println("1--" + a1.show(b));
            System.out.println("2--" + a1.show(c));
            System.out.println("3--" + a1.show(d));
            System.out.println("4--" + a2.show(b));
            System.out.println("5--" + a2.show(c));
            System.out.println("6--" + a2.show(d));
            System.out.println("7--" + b.show(b));
            System.out.println("8--" + b.show(c));
            System.out.println("9--" + b.show(d));      
        }
}
class A {
    public String show(D obj) {
        return ("A and D");
    }

    public String show(A obj) {
        return ("A and A");
    } 

}
class B extends A{
    public String show(B obj){
        return ("B and B");
    }

    public String show(A obj){
        return ("B and A");
    } 
}
 class C extends B{

}
 class D extends B{

}

解释
a1.show(b));Class A 中没有show(B obj),B转向B的父类A,执行A show(A obj)—>return “A and A”
a1.show(c));Class A 中没有show(C obj),C转向C的父类B,Class A 中没有show(B obj),再转向父类A,执行A show(A obj)—>return “A and A”
a1.show(d));Class A 中有show(D obj)执行A show(D obj)—>return “A and D”
这个比较特殊:A a2 = new B();父类声明,子类实例,你应该把a2当作子类重写完后的父类看,注意只有父类的方法。
a2.show(b));Class A 中没有show(B obj),B转向B的父类A,执行A show(A obj),A的show 方法被重写,执行B show(A obj)—>return “B and A”
a2.show(c));Class A 中没有show(C obj),C转向C的父类B,Class A 中没有show(B obj),B转向父类A,执行A show(A obj),A的show 方法被重写,执行B show(A obj)—>return “B and A”
a2.show(d));Class A 中有show(D obj)执行A show(D obj)—>return “A and D”
b.show(b)); Class B 中有show(B obj)—>return “B and B”
b.show(c)); Class B 中没有show(C obj),C转向C的父类B,执行B show(B obj)—>return “B and B”
b.show(d)); Class B 中有继承了Class A 的show(D obj),执行A show(D obj)—>return “A and D”

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中多态是一种重要的面向对象编程特性,它允许一个对象以多种不同的方式工作。下面介绍一个经典的Java多态案例——“动物园”。 假设有一个“动物园”类,里面有一个“动物”类,还有“狗”和“猫”两个子类,代码如下: ```java class Zoo { public void play(Animal animal) { animal.shout(); } } class Animal { public void shout() { System.out.println("动物叫!"); } } class Dog extends Animal { public void shout() { System.out.println("汪汪汪!"); } } class Cat extends Animal { public void shout() { System.out.println("喵喵喵!"); } } ``` 在“动物园”类中有一个play方法,它接收一个“动物”类对象作为参数,然后调用这个对象的shout方法。在“狗”和“猫”两个子类中分别重写了shout方法,实现了不同的叫声。 这时候我们可以创建一个“动物园”对象,然后分别将“狗”和“猫”对象传递给play方法进行测试。代码如下: ```java public class Main { public static void main(String[] args) { Zoo zoo = new Zoo(); Animal dog = new Dog(); Animal cat = new Cat(); zoo.play(dog); zoo.play(cat); } } ``` 运行结果如下: ``` 汪汪汪! 喵喵喵! ``` 通过这个案例,我们可以看到,在“动物园”类中,play方法接收一个“动物”类对象作为参数,而在实际测试中我们传递的是“狗”和“猫”对象,这就是多态的应用。我们可以看到,即使传递的是不同的对象,调用的都是各自重写的shout方法,实现了不同的叫声。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值