总结,接口可以在一个类继承别的父类后,如果父类不满足当前的需求,可以通过接口的形式添加方法,进行功能的扩充
public class Inter_Demo {
public static void main(String[] args) {
Dog a1 = new Dog();
a1.eat();
JumpDog a2 = new JumpDog();
a2.jump();
JumpCat a3 = new JumpCat();
a3.jump();
}
}
abstract class Animal{
abstract void eat(); // 吃的抽象方法
abstract void work(); // 工作的抽象方法
public void sleep(){
System.out.println("动物要睡觉了");
}
}
class Dog extends Animal{
@Override
void eat() {
System.out.println("狗吃肉");
}
@Override
void work() {
System.out.println("猫吃鱼");
}
}
class Cat extends Animal{
@Override
void eat() {
System.out.println("猫吃鱼");
}
@Override
void work() {
System.out.println("猫抓老鼠");
}
}
interface HighJump{
abstract void jump(); // 可以不用前面加abstract
}
class JumpDog extends Dog implements HighJump{
@Override
public void jump() {
System.out.println("狗狗学会调高了");
}
}
class JumpCat extends Cat implements HighJump{
@Override
public void jump() {
System.out.println("猫也学会了跳高。。。。。。。");
}
}
标签:java,void,System,接口,Override,应用,println,class,out
From: https://www.cnblogs.com/ch2020/p/16852102.html