使用接口实现一部手机
手机类代码:
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(); } }
运行结果: