先创建一个父类Animal类:

public class Animal{
    public void move(){
        System.out.println("动物可以移动");
    }
}

创建子类Cat类:

public class Cat extends Animal{
    public void move(){     //继承Animal类,然后重写move方法;
        System.out.println("cat can run");
    }
    public void catcatch(){     //这是cat特有的方法
        System.out.println("cat can catch mouse");
    }
}

再创建一个子类Bird类:

public class Bird extends Animal{
    public void move(){
        System.out.println("Bird can fly");
    }
}

在AnimalTest测试类中:

/*
多态的基础语法使用
*/

public class AnimalTest{
    public static void main(String[] args){
        Animal a = new Animal();
        a.move();
        
        Cat c = new Cat();
        c.move();
        
        Bird b = new Bird();
        b.move();
        
        Animal c1 = new Cat();   //向上转型 ,可以创建 , Cat就是一个Animal
        //c1.catcatch();   //不能编译通过 , 在编译阶段,只会在Animal.class中找catcatch方法,没有找到
        Cat x = (Cat)c1;   //当需要访问子类中特有的方法时, 此时就需要向下类型转换
        x.catcatch();
        
        //向下类型转换的风险
        /*
        Animal b1 = new Bird();
        Cat y = (Cat)b1;    //编译阶段不会报错,在编译阶段只识别到b1是一个Animal类型;
        y.catcatch();       //但是在运行阶段就会报错,java.lang.ClassCastException:类型转化异常,因为Bird和Cat没有继承关系;
        */
        
        Animal b2 = new Bird();
        //为了规避这种风险,可以使用instanceof进行判断,只有b2是一个Cat类型时,才会进行向下转型
        if(b2 instanceof Cat){
            Cat z = (Cat)b2;
            z.catcatch();
        }
        
    }
}

在示例向下转型过程中:

  在编译是,只识别到b1是一个一个Animal类型,然后向下转型为Cat,而Animal和Cat之前存在着继承关系,就可以进行向下转型,所以编译没有问题;

而在运行态时,在堆内存中实际创建了Bird对象,Bird对象与Cat对象没有继承关系,所以就不能进行类型转化;就会发生java.lang.ClassCastException:类型转化异常;

为了解决类型转化异常可以使用instanceof在运行时进行判断:

 (1)instanceof可以在运行期动态判断引用指向对象的类型;

 (2)instanceof语法结构(引用 instanceof 类型);

   (3)instanceof返回值为false/true;

   (4)b2是一个引用,b2变量保存的内存地址指向了堆内存中对象,(b2 instanceof Cat)为true,则表示是一个Cat类型,为false则相反;

posted on 2021-01-03 11:16  homle  阅读(385)  评论(0编辑  收藏  举报