接口和继承
/* * 类与类: 继承关系,只能单继承,可以多层继承 类与接口: 实现关系,可以单实现,也可以多实现 接口与接口: 继承关系,可以单继承,也可以多继承 * */ interface Father{ public abstract void show(); } interface Mother{ public abstract void show2(); } interface Sister extends Father, Mother{ } class Son extends Object implements Father, Mother{ public void show(){ System.out.println("show son"); } public void show2(){ System.out.println("show son2"); } } public class InterfaceDemo2 { public static void main(String[] args){ } }
抽象类使用接口进行功能扩展
interface Jumpping{ public abstract void jump(); } abstract class Animal{ private String name; private int age; public Animal(){} public Animal(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 void sleep(){ System.out.println("睡觉啰"); } public abstract void eat(); } class Cat extends Animal{ public Cat(){} public Cat(String name, int age){ super(name, age); } public void eat(){ System.out.println("猫吃鱼"); } } class JumpCat extends Cat implements Jumpping{ public JumpCat(){} public JumpCat(String name, int age){ super(name, age); } public void jump(){ System.out.println("猫跳高"); } } public class InterfaceDemo3 { public static void main(String[] args){ JumpCat jp = new JumpCat(); jp.setName("Tom"); jp.setAge(3); System.out.println(jp.getName()+"------"+jp.getAge()); jp.eat(); jp.sleep(); jp.jump(); } }
原文:http://www.cnblogs.com/rain-1/p/5062850.html