接口,抽象类的一个demo
#定义一个抽象类 abstract class Ou { #成员变量 private String name; private int age; #空参构造方法 public Ou(){} #有参构造方法 public Ou(String name,int age){ this.name = name; this.age = age; } public void setName(String name){ this.name = name; } public String getName(){ return name; } public void setAge(int age){ this.age = age; } public int getAge(){ return age; } #抽象方法 public abstract void eat(); public abstract void sleep(); } #定义接口 interface Jumping{ public void jump(); } #定义Mimi类继承抽象类Ou class Mimi extends Ou{ public Mimi(){} public Mimi(String name,int age){ super(name,age); } public void eat(){ System.out.println("猫吃饭"); }; public void sleep(){ System.out.println("猫爱睡觉"); } } #定义JumpCat继承Mimi类并实现Jumping接口 class JumpCat extends Mimi implements Jumping{ public JumpCat(){} public JumpCat(String name,int age){ super(name,age); } public void jump(){ System.out.println("猫跳高"); } } public class Test_interFace { public static void main(String[] args){ Mimi m = new Mimi("加肥",8); m.eat(); m.sleep(); JumpCat jc = new JumpCat("蛋黄",4); jc.eat(); jc.jump(); jc.sleep(); } }