1 package cn.bjsxt.oop.testInterface; 2 /** 3 * 接口 是最抽象的类 只有声明 实现交给实现类 设计合实现分离 4 * 只有常量和抽象方法 JDK1.8之后 接口里也可以有普通方法(非抽象方法) 但必须加上default public default void test(){方法体} 5 * @author Administrator 6 *容易分工 各自实现 7 */ 8 public interface MyInterface { 9 //只有常量和抽象方法10 //接口中定义常量 public static final 是默认的 写不写都有11 /*public static final */String MAX_GRADE = "BOSS";12 int MAX_SPEED = 120;13
JDK1.8之后 接口里也可以有普通方法(非抽象方法) 但必须加上default
public default void test(){方法体}
14 /*public abstract*/ void test01(); 15 /*public abstract*/ int test02(int a,int b); 16 17 18 }
1 package cn.bjsxt.oop.testInterface; 2 3 public class MyClass implements MyInterface { 4 5 @Override 6 public void test01() { 7 // 8 System.out.println("test01"); 9 }10 11 @Override12 public int test02(int a, int b) {13 // TODO Auto-generated method stub14 return a+b;15 }16 17 }
1 package cn.bjsxt.oop.testInterface; 2 /** 3 * 实现类可以实现多个接口 4 * @author Administrator 5 * 6 */ 7 public interface Flyable { 8 int MAX_SPEED=11000; 9 int MIN_HEIGHT=1;10 /*public*/void fly();11 }12 interface Attack{13 void hit();14 }15 class Plane implements Flyable{16 17 @Override18 public void fly() {19 // TODO Auto-generated method stub20 System.out.println("飞机依靠发动机飞行");21 }22 23 }24 class Men implements Flyable,Attack{25 26 @Override27 public void fly() {28 // TODO Auto-generated method stub29 System.out.println("跳起来。飞");30 }31 32 @Override33 public void hit() {34 // TODO Auto-generated method stub35 System.out.println("拳头打人");36 }37 38 }39 class Stone implements Flyable,Attack{40 41 @Override42 public void fly() {43 // TODO Auto-generated method stub44 System.out.println("被人扔出去飞");45 }46 47 @Override48 public void hit() {49 // TODO Auto-generated method stub50 System.out.println(" 拿起石头攻击");51 }52 53 }
1 package cn.bjsxt.oop.testInterface; 2 /** 3 * 接口可以多继承 4 * @author Administrator 5 *实现多继承接口 要实际所有继承的接口 6 */ 7 public interface InterfaceA { 8 void aaa(); 9 }10 interface InterfaceB{11 void bbb();12 }13 interface InterfaceC extends InterfaceA,InterfaceB{14 void ccc();15 }16 class TestClass implements InterfaceC{17 18 @Override19 public void aaa() {20 // TODO Auto-generated method stub21 22 }23 24 @Override25 public void bbb() {26 // TODO Auto-generated method stub27 28 }29 30 @Override31 public void ccc() {32 // TODO Auto-generated method stub33 34 }35 36 }