自学java (day23):接口(interface)练习

/**
 * 设计一个笔记本电脑类,属性随意,并且进行属性私有化,对外提供公开的getter和setter方法
 *
 * 设计一个可插拔接口:InsertDrawable,该接口有什么方法自行定义
 *
 * 设计一个鼠标类,实现InsertDrawable接口,并实现方法
 * 设计一个键盘类,实现InsertDrawable接口,并实现方法
 * 设计一个显示器类,实现InsertDrawable接口,并实现方法
 * 设计一个打印机类,实现InsertDrawable接口,并实现方法
 *
 * 在“笔记本电脑类”中有一个InsertDrawable接口属性,可以让笔记本电脑可以插拔鼠标、键盘、显示器、打印机
 *
 * 编写测试程序,创建多个对象,演示接口的作用
 */

//测试程序
public class Computer{
    public static void main(String[] args){
        //创建对象
        InsertDrawable id=new shubiao();
        Computer1 c=new Computer1(id);
        c.ysb();



    }
}

//笔记本电脑类
class Computer1 {
    //可插拔属性
    private InsertDrawable insertDrawable;

    //构造方法
    public Computer1(){

    }

    public Computer1(InsertDrawable insertDrawable){
        this.insertDrawable=insertDrawable;
    }

    //getterANDsetter

    public void setInsertDrawable(InsertDrawable insertDrawable) {
        this.insertDrawable = insertDrawable;
    }

    public InsertDrawable getInsertDrawable() {
        return insertDrawable;
    }

    //用鼠标的方法
    public void ysb(){
        insertDrawable.yongshubiao();
        insertDrawable.yongjianpan();
    }
}

//接口
interface InsertDrawable{
    void yongshubiao();
    void yongjianpan();
}

//鼠标类
class shubiao implements InsertDrawable{

    public void yongshubiao() {
        System.out.println("用鼠标");
    }

    public void yongjianpan(){
        System.out.println("用键盘");
    }
}

//键盘类
class jianpan{

}

//显示器类
class xianshiqi{

}

//打印机类
class dayinji{

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值