Java学习笔记 抽象类 接口 多态

instanceof

对象名 instanceof 类名code

该对象是否属于该类对象

Animal animal = new Dog();
    if(animal instanceof Dog){
        Dog d = (Dog)animal;
    }

多态

Animal animal = new Dog();
    animal.sleep();//先调用子方法,若是子方法未重写(复写),则执行父类中的该方法

抽象类

子类继承父类,父类是个抽象类,子类必须实现父类中的抽象方法,若是不想实现,能够将子类定义为抽象类,让下一个继承子类的类来实现继承

接口

接口里面只能存放常量和抽象方法接口

定义的变量会默认添加public static final这些关键字变量

方法也是会自动添加abstract方法

例如:
int numer = 10; 其实至关于 public static final int numer = 10;im

publlic void hello(); 至关于 public abstract void hello();static

接口能够实现多继承co

接口也能够实现多态new

Eat eat = new Dog();
    eat.print(Eat eat);//狗实现了吃的接口,实现了吃接口里面的print方法,以后调用eat.print(),执行的是狗实现的print方法

    void print(Eat eat){
        eat.print();
    }
相关文章
相关标签/搜索