07 多态/instanceof

多态

  • 多态是同一个行为具有多个不同表现形式或形态的能力。
  • 多态是方法的多态,属性没有多态
  • 多态存在的三个必要条件:继承、重写、父类引用指向子类对象(Parent p=new Child())

向上转型

子类引用的对象转换为父类类型称为向上转型。

  • 向上转型时,子类单独定义的方法会丢失。能调用的方法:看左边引用对象的方法。
  • 子类引用不能指向父类对象。Cat c = (Cat)new Animal()这样是不行的。
public class Animal {
    public void eat(){
        System.out.println("animal eatting...");
    }
}
 
public class Cat extends Animal{
 
    public void eat(){
 
        System.out.println("我吃鱼");
    }
}
 
public class Dog extends Animal{
 
    public void eat(){
 
        System.out.println("我吃骨头");
    }
 
    public void run(){
        System.out.println("我会跑");
    }
}
 
public class Main {
 
    public static void main(String[] args) {
 
        Animal animal = new Cat(); //向上转型
        animal.eat();
 
        animal = new Dog();
        animal.eat();
    }
 
}
 
//结果:
//我吃鱼
//我吃骨头

向下转型

向下转型是把父类对象转为子类对象

  • 向下转型的前提是父类对象指向的是子类对象(也就是说,在向下转型之前,它得先向上转型
  • 向下转型只能转型为本类对象(猫是不能变成狗的)。
//还是上面的animal和cat dog
Animal a = new Cat();
Cat c = ((Cat) a);
c.eat();
//输出  我吃鱼
Dog d = ((Dog) a);
d.eat();
// 报错 : java.lang.ClassCastException:com.chengfan.animal.Cat cannot be cast to com.chengfan.animal.Dog
//因为 a 本身就是 Cat 对象,所以它理所当然的可以向下转型为 Cat,也理所当然的不能转为 Dog
Animal a1 = new Animal();
Cat c1 = ((Cat) a1);
c1.eat();
// 报错 : java.lang.ClassCastException:com.chengfan.animal.Animal cannot be cast to com.chengfan.animal.Cat
// a1 为 Animal 对象,它也不能被向下转型为任何子类对象


  • 当父类对象引用变量引用子类对象时,被引用对象的类型决定了调用谁的成员方法,引用变量类型决定可调用的方法。如果子类中没有覆盖该方法,那么会去父类中寻找

  • 继承链中对象方法的调用的优先级:this.show(O)、super.show(O)、this.show((super)O)、super.show((super)O)。

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{
 
}
 
public class Demo {
    public static void main(String[] args) {
        A a1 = new A();//能调用的方法都是自己的
        A a2 = new B();//能调用的方法是A的和B中重写A的,不能调用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));
    }
}
//结果:
//1--A and A
//2--A and A
//3--A and D
//4--B and A
//5--B and A
//6--A and D
//7--B and B
//8--B and B
//9--A and D



instanceof

  • instanceof 是 Java 的一个二元操作符,类似于 ==,>,< 等操作符。

  • instanceof 是 Java 的保留关键字。它的作用是测试它左边的对象是否是它右边的类的实例,返回 boolean 的数据类型。

public class A{
    public static void main(String[] args){
        //Object > String
        //Object > Person > Teacher
        //Object > Person > Student
        Object object =new Student();
        
        System.out.println(object instanceof Student);//true
        System.out.println(object instanceof Person);//true
        System.out.println(object instanceof Object);//true
        System.out.println(object instanceof Teacher);//false
        System.out.println(object instanceof String);//false
        //============================================
        Person person=new Student();
        System.out.println(person instanceof Student);//true
        System.out.println(person instanceof Person);//true
        System.out.println(person instanceof Object);//true
        System.out.println(person instanceof Teacher);//false
        System.out.println(person instanceof String);//编译报错
        //===============================================
        Student student=new Student();
        System.out.println(student instanceof Student);//true
        System.out.println(student instanceof Person);//true
        System.out.println(student instanceof Object);//true
        System.out.println(student instanceof Teacher);//编译报错
        System.out.println(student instanceof String);//编译报错
    }
}
posted @ 2020-12-23 20:12  迪迦是真的  阅读(128)  评论(0)    收藏  举报
//复制代码按钮 //代码行号 //评论