package class_ex;
/**
* ç¹¼æ¿ interface ç¯ä¾
*
*/
public class InterfaceClassExample {
public static void main(String[] args) {
// Airplane, Bird example.
Fly bird = new Bird("angry bird"); // upcasting
bird.flying();
bird.getName();
Fly airplane = new Airplane("Boeing 747 airplane"); // upcasting
airplane.flying();
airplane.getName();
// Car example. (主è¦çè§£default, static, private method in the interface class.)
Car car = new Car("Toyota");
System.out.println("[Step 1] " + car.alarmOn());
System.out.println("[Step 2] " + car.getBrand());
System.out.println("[Step 3] " + car.run());
System.out.println("------------------------------------");
Car result = car.execute();
System.out.println("execute time: " + result.getExecuteTime());
}
}
/**
* 使ç¨å®ä¸ interface ä»é¢ä¾å¯¦ä½
* - interface: Fly
* - class: Bird, Airplane
*/
interface Fly {
void getName();
void flying();
}
class Bird implements Fly {
private String name;
Bird(String name) {
this.name = name;
}
@Override
public void getName() {
System.out.println("I am a " + name + ".");
}
@Override
public void flying() {
System.out.println("Bird is flying...");
}
}
class Airplane implements Fly {
private String name;
Airplane(String name) {
this.name = name;
}
public void getName() {
System.out.println("I am a " + name + ".");
}
public void flying() {
System.out.println("Airplane is flying...");
}
}
/**
* Interface class è¦åèç¯ä¾
* - ä»é¢å
§ private method å卿ï¼å¯æä¾ä»é¢å
§ç¨å¼ç¢¼éè¤ä½¿ç¨ã
* - 實ä½çé¡å¥å°ç¡æ³å¼å«ä»é¢å
§ private methodã
* - default method åå¯ä»¥è¢«å¯¦ä½é¡å¥å¼å«ã
*
* default æ´å
interfaceæ°åè½ï¼å¦æä¸ä½¿ç¨ defaultï¼interfaceä¸å
è¨±æ¹æ³å
§æå¯¦ä½å
§å®¹ã
*/
interface Vehicle {
String getBrand();
String run();
default String alarmOn() {
method1();
return "turn on alarm.";
}
default String alarmOff() {
return "turn off alarm.";
}
static int speedUp(int speed) {
return speed + 50;
}
/**
* æ¤ private method åªè½è¢«å
許å¨ä»é¢å
§ä½¿ç¨
*/
private void method1() {
method3();
System.out.println("This is a method 1.");
}
private void method2() {
System.out.println("This is a method 2.");
}
private void method3() {
System.out.println("This is a method 3.");
method2();
}
/**
* TODO: æ¯å¦å¯ä»¥ä½¿ç¨éç¨ä»é¢ä¾è®classç¹¼æ¿ä¸¦å¯¦ä½ç´°ç¯
*/
public Vehicle> execute();
}
/**
* å¯¦ä½ interface: Vehicle + Generic
*/
class Car implements Vehicle {
private String brand;
private int execute_time;
Car(String brand) {
this.brand = brand;
}
@Override
public String getBrand() {
return brand;
}
@Override
public String run() {
return "driving car safely.";
}
public Car step4() {
System.out.println("call step 4.");
execute_time++;
return this;
}
public Car step5() {
System.out.println("call step 5.");
execute_time++;
return this;
}
public Car step6() {
System.out.println("call step 6.");
execute_time++;
return this;
}
public Car reverseStep() {
System.out.println(" call reverse step.");
execute_time--;
return this;
}
@Override
public Car execute() {
execute_time = 0;
this.step4()
.reverseStep()
.step5()
.step6()
.reverseStep();
// System.out.println("execute_time: " + execute_time);
return this;
}
public int getExecuteTime() {
return this.execute_time;
}
}