使用接口实现一部手机

手机类代码:

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();
    }
}

运行结果:

 

转载于:https://www.cnblogs.com/yangwenxiang/p/10399181.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值