定义接口Fly,飞机(plane)与鸟(brid)实现接口完成飞。
package New.Week.JieKo;
public interface Fly {
void Flying();
}
class Plane implements Fly {
private String name;
public Plane(String name) {
this.name = name;
}
@Override
public void Flying() {
System.out.println(this.name+"开始飞行");
}
}
class Brid implements Fly {
private String name;
public Brid(String name) {
this.name = name;
}
@Override
public void Flying() {
System.out.println(this.name+"扇动飞行");
}
}
package New.Week.JieKo;
public class Test {
public static void testFly(Fly f) {
f.Flying();
}
public static void main(String[] args) {
testFly(new Plane("国产飞机"));
testFly(new Brid("鹦鹉"));
}
}
运行结果:
国产飞机开始飞行
鹦鹉扇动飞行
Process finished with exit code 0