手機類代碼:
package com.phone; public abstract class Phone{ private String brand; private String type; public String getBrand() { return brand; } public String getType() { return type; } public void setBrand(String brand) { this.brand = brand; } public void setType(String type) { this.type = type; } public void Phone(){} public void Phone(String brand,String type){ this.brand = brand; this.type = type; } public void show(){ System.out.println("這是一部"+brand+type+"手機"); } public void call(){ System.out.println("------開始打電話"); } public void message(){ System.out.println("------開始發短信"); } }
普通手機類代碼:
package com.phone; public class CommonPhone extends Phone implements PlayingWiring{ public void play(String name){ System.out.println("------開始播放音頻:"+name); } }
智能手機類代碼:
package com.phone; public class AptitudePhone extends Phone implements Net,PlayingWiring,TakePhotos{ public void internet(){ System.out.println("------開始上網"); } public void play(String name){ System.out.println("------開始播放視頻:"+name); } public void photo(){ System.out.println("------開始拍照"); } }
上網接口代碼:
package com.phone; public interface Net { public void internet(); }
音頻接口代碼:
package com.phone; public interface PlayingWiring { public void play(String name); }
照相接口代碼:
package com.phone; public interface TakePhotos { public void photo(); }
測試類代碼:
package com.phone; public class TestPhone { public static void main(String[] args) { CommonPhone commonPhone = new CommonPhone(); commonPhone.Phone("諾基亞","3180"); commonPhone.show(); commonPhone.call(); commonPhone.message(); commonPhone.play("小蘋果"); System.out.println("******************************"); AptitudePhone aptitudePhone = new AptitudePhone(); aptitudePhone.Phone("iphone","xs"); aptitudePhone.show(); aptitudePhone.call(); aptitudePhone.message(); aptitudePhone.play("泰坦尼克號"); aptitudePhone.internet(); aptitudePhone.photo(); } }
運行結果:

