Main类:
public class Main {
public static void main(String[] args) {
Animal animalDog = new Dog();
Animal animalCat = new Cat();
animalDog.eat();
animalCat.eat();
if (animalCat instanceof Cat) {
Cat cat = (Cat) animalCat;
}
//使用匿名内部类
OutPut out = new OutPut(){
@Override
public void showA() {
System.out.println("OutPut->showA()");
}
@Override
public void showB() {
System.out.println("OutPut->showB()");
}
@Override
public void showDown() {
System.out.println("OutPut->showDown()");
}
};
out.showC();
out.showA();
out.showB();
}
}
/*
接口的特点:
1.关键字为interface(是什么)
2 修饰符只能是public(前缀)
3.接口可以同时继承多个父接口(后缀)
4.接口中的属性只能是常量(不允许有变量) (里面的常量)
5.接口中的方法默认的修饰符是Public ()
6.接口中定义非抽象方法(默认)使用default进行修饰
*/
interface interfaceA{
void showA();
}
interface interfaceB{
void showB();
}
public interface OutPut extends interfaceA,interfaceB{
int AGE =10;//常量
public default void showC(){System.out.println("OutPut->showC()");}
void showDown();
}
animal类:
public abstract class Animal {
int nAge;
String strName;
//普通方法
public void eat(){System.out.println("Animal->eat()");}
//内部类
class Inner{
}
//抽象方法,只能默认和public修饰
abstract void Sleep();
}
cat类:
public class Cat extends Animal implements interfaceA,interfaceB {
@Override//override重写
void Sleep() {
}
@Override
public void eat() {
System.out.println("Cat->eat()");
}
@Override
public void showA() {
}
@Override
public void showB() {
}
}
Dog类:
public class Dog extends Animal implements OutPut {
@Override
public void eat() {
System.out.println("Dog->eat");
}
@Override
void Sleep() {
}
@Override
public void showA() {
}
@Override
public void showB() {
}
@Override
public void showDown() {
}
}