public interface EletronicDevice { public void turnOn(); public void turnOff(); }
使用方式:
public class Television implements EletronicDevice{ @Override public void turnOn() { System.out.println("電視機開啟中..."); initializeScreen(); } @Override public void turnOff() { initializeScreen(); System.out.println("電視機關閉中..."); } public void changeChannel(int channel){ System.out.printf("切換至第 %d 台", channel); } private void initializeScreen(){ System.out.println("清除螢幕視窗...."); } }
執行方式:
public class Demo { public static void main(String[] args){ EletronicDevice ed = new Television(); ed.turnOn(); //介面與類別相同的功能:casting ((Television)ed).changeChannel(2); ed.turnOff(); String s = ed.toString(); System.out.println(s); } }