Java 接口——4
package lianxi3; public interface CanSwim { void swim(); }
package lianxi3; public interface Canfly { void fly(); }
package lianxi3; public abstract class Actioncharacter { //抽象方法、 void fight(String emp) { System.out.println(emp); } void speak(String s) { System.out.println(s); } }
package lianxi3; public class Hero extends Actioncharacter implements Canfly, CanSwim { @Override public void swim() { // TODO 自动生成的方法存根 System.out.println(name+"游泳"); } @Override public void fly() { // TODO 自动生成的方法存根 System.out.println(name+"飞翔"); } String name; Hero (String name) { this.name=name; } }
package lianxi3; public class ClassAdventure { public static void main(String[] args) { // TODO 自动生成的方法存根 Hero hb=new Hero("张三"); hb.swim(); hb.fly(); hb.fight(null); Canfly cf=new Hero("李四"); // cf=hb; cf.fly(); CanSwim cs=new Hero("小明"); // cs=hb; cs.swim(); Actioncharacter ac=new Hero("小红"); // ac=hb; ac.fight("emp"); ac.speak("中文"); } }