接口---火箭 石头 的关系(更抽象的抽象类)
1 package cn.xlf.oop.testInterface; 2 3 public interface Flyable { 4 int MAX_SPEED = 110000; 5 int MIN_HEIGHT = 1; 6 void fly(); 7 8 } 9 interface Attack{ 10 void attack(); 11 } 12 class Plane implements Flyable{ 13 14 public void fly(){ 15 System.out.println("By machine"); 16 } 17 } 18 class Man implements Flyable{ 19 public void fly(){ 20 System.out.println("By foot"); 21 } 22 } 23 class Stone implements Flyable,Attack{ 24 public void fly(){ 25 System.out.println("被扔出去"); 26 } 27 public void attack(){ 28 System.out.println("石头进攻"); 29 } 30 }
Flyable接口
1 package cn.xlf.oop.testInterface;
2
3 public class TestFlyable {
4 public static void mian(String args){
5 Flyable f = new Stone();
6 f.fly(); }
7
8 }
Attack接口
1 package cn.xlf.oop.testInterface;
2
3 public class TestFlyable {
4 public static void main(String[] args){
5 Attack f = new Stone();
6 f.attack();
7 }
8
9 }