Java编程---设计一个笔记本电脑类,属性随意,并且进行属性私有化,对外提供公开的set和get方法。设计一个可插拔的接口:InsertDrawable,该接口有什么方法自行定义。设计一个鼠标类.

题目:开放型题目:
设计一个笔记本电脑类,属性随意,并且进行属性私有化,对外提供公开的set和get方法。

设计一个可插拔的接口:InsertDrawable,该接口有什么方法自行定义。
设计一个鼠标类,实现InsertDrawable接口,并实现方法。
设计一个键盘类,实现InsertDrawable接口,并实现方法。
设计一个显示器类,实现InsertDrawable接口,并实现方法。
设计一个打印机类,实现InsertDrawable接口,并实现方法。

在“笔记本电脑类”中有一个InsertDrawable接口属性,可以让笔记本
电脑可插拔鼠标、键盘、显示器、打印机等。

编写测试程序,创建多个对象,演示接口的作用。
public class ComputerTest {
    public static void main(String[] args) {
        //创建鼠标、键盘、显示器、打印机接口对象
        InsertDrawable mouse = new Mouse();
        InsertDrawable keyboard = new keyBoard();
        InsertDrawable display = new disPlay();
        InsertDrawable print = new Print();
        //创建电脑对象
        Computer computer = new Computer();
        //将鼠标、键盘、显示器、打印机接入电脑
        computer.getSlot()[0]=mouse;
        computer.getSlot()[1]=keyboard;
        computer.getSlot()[2]=display;
        computer.getSlot()[3]=print;
        //运行
        computer.getSlot()[0].work();
        computer.getSlot()[1].work();
        computer.getSlot()[2].work();
        computer.getSlot()[3].work();
    }
}
//笔记本电脑类
class Computer{
    //电脑接口
    private  InsertDrawable slot[];
    //无参构造方法(默认为五个接口)
    public Computer(){
        this(new InsertDrawable[5]);
    }
    //有参构造方法
    public Computer(InsertDrawable[] slot) {
        this.slot = slot;
    }

    //setter and getter
    public InsertDrawable[] getSlot() {
        return slot;
    }
    public void setSlot(InsertDrawable[] slot) {
        this.slot = slot;
    }
}

//接口
interface InsertDrawable{
    public abstract void work();
}

//鼠标类
class Mouse implements InsertDrawable{
    public  void work(){
        System.out.println("鼠标成功接入电脑,正常工作!");
    };
}

//键盘类
class keyBoard implements InsertDrawable{
    public  void work(){
        System.out.println("键盘成功接入电脑,正常工作!");
    };
}

//显示器类
class disPlay implements InsertDrawable{
    public  void work(){
        System.out.println("显示器成功接入电脑,正常工作!");
    };
}

//打印机类
class Print implements InsertDrawable{
    public  void work(){
        System.out.println("打印机成功接入电脑,正常工作!");
    };
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值