多态的转型

向上转型(自动类型的提升)

多态的正常使用

向下转型(强制类型转换)

解决问题 父类引用不能使用子类中特有的成员

子类  对象 = (子类)  父引用

package PolymorphicInnerClass.demo2;

/**
 * @author xiaowang
 * @creat 2024/6/2 14:46
 * @Description Java Lotus
 */
public class Test {
    public static void main(String[] args) {
        Father f=new Son();
        System.out.println(f.num);

        f.show();

//        f.show2();检查父类中的成员方法
        ((Son)f).show2();
    }
}

向下转型的注意事项

java.lang.ClassCastException

容易发生类型转换的异常

package PolymorphicInnerClass.demo3;

/**
 * @author xiaowang
 * @creat 2024/6/2 14:59
 * @Description Java Lotus
 */
public abstract class Animal {
    private String bread;
    private String color;

    public Animal(){

    }

    public Animal(String bread, String color) {
        this.bread = bread;
        this.color = color;
    }

    public String getBread() {
        return bread;
    }

    public void setBread(String bread) {
        this.bread = bread;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public abstract void eat();
}


package PolymorphicInnerClass.demo3;

/**
 * @author xiaowang
 * @creat 2024/6/2 15:00
 * @Description Java Lotus
 */
public class Cat extends Animal{
    public Cat() {
    }

    public Cat(String bread, String color) {
        super(bread, color);
    }

    @Override
    public void eat(){
        System.out.println("猫吃鱼");
    }

    //子类特有方法
    public void catchMouse(){
        System.out.println("抓老鼠");
    }




}




package PolymorphicInnerClass.demo3;

/**
 * @author xiaowang
 * @creat 2024/6/2 15:00
 * @Description Java Lotus
 */
public class Dog extends Animal{

    public Dog() {
    }

    public Dog(String bread, String color) {
        super(bread, color);
    }

    @Override
    public void eat() {
        System.out.println("狗吃骨头");
    }

    public void lookDoor(){
        System.out.println("狗看家");
    }
}


package PolymorphicInnerClass.demo3;

/**
 * @author xiaowang
 * @creat 2024/6/2 15:00
 * @Description Java Lotus
 */
public class Pig extends Animal{

    public Pig() {
    }

    public Pig(String bread, String color) {
        super(bread, color);
    }


    @Override
    public void eat() {
        System.out.println("猪拱白菜");
    }

    public void sleep(){
        System.out.println("一直睡觉");

    }
}




package PolymorphicInnerClass.demo3;

/**
 * @author xiaowang
 * @creat 2024/6/2 15:00
 * @Description Java Lotus
 */
public class Test {
    public static void main(String[] args) {
        useAnimal(new Cat());

        System.out.println("------------");

        useAnimal(new Dog());

        System.out.println("------------");

        useAnimal(new Pig());

        System.out.println("------------");
    }



//    public static void useAnimal(Cat c){
//        c.eat();
//        c.catchMouse();
//    }
//
//    public static void useAnimal(Dog d){
//        d.eat();
//        d.lookDoor();
//    }
//
//    public static void useAnimal(Pig p){
//        p.eat();
//        p.sleep();
//    }

    public static void useAnimal(Animal animal){
        animal.eat();

        if(animal instanceof Cat){
            Cat cat=(Cat) animal;
            cat.catchMouse();
        }
        if(animal instanceof Dog){
            Dog dog=(Dog) animal;
            dog.lookDoor();
        }
        if(animal instanceof Pig){
            Pig pig=(Pig) animal;
            pig.sleep();
        }
    }

}







  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值