java练习题-笔记本(接口实现)

题目:

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

设计一个可插拔的接囗:InsertDrawab1e,该接囗有什么方法自行定义。

设计一个鼠标类,实现InsertDrawable接口,并实现方法。
设计一个键盘类,实现InsertDrawable接口,并实现方法。
设计一个显示器类,实现InsertDrawable接口,并实现方法。

设计一个打印机类,实现接口,并实现方法。
在"笔记本电脑类“中有一个InsertDrawle接口属性,
可以让笔记本电脑可插拔鼠标、键盘、显示器、打印机等。

编写测试程序,创建多个对象,演示接囗的作用。

package com.bjPowerNode.HomeWork.HomeWork01;

//演示接口作用
public class ComputerTest {
    public static void main(String[] args) {
        //调用有参构造新建电脑对象(此处并未考虑电脑最多连接设备的数量)
        Compter compter1 = new Compter("张三","123456","2022-01-19",12);

        //创建鼠标对象 并将其赋给compter1.mouse
        Mouse mouse1 = new Mouse();
        compter1.mouse = mouse1;
        //使用compter1.mouse,调用Mouse类中的方法
        compter1.mouse.Insert();
        compter1.mouse.Draw();

        //创建键盘对象 调用类中方法
        Keyboard keyboard1 = new Keyboard();
        compter1.keyboard = keyboard1;
        compter1.keyboard.Insert();
        compter1.keyboard.Draw();

        //创建显示器对象 调用类中的方法
        Monitor monitor1 = new Monitor();
        compter1.monitor = monitor1;
        compter1.monitor.Insert();
        compter1.monitor.Draw();

        //创建打印机对象 调用类中的方法
        Priter priter1 = new Priter();
        compter1.priter = priter1;
        compter1.priter.Insert();
        compter1.priter.Draw();
    }
}

//电脑类
class Compter {
    //私有变量 名称 出厂编号 生产日期 保修期
    private String name;
    private String id;
    private String productDate;
    private int data;
    //电脑与鼠标 键盘 显示器 打印机 设备的关系
    Mouse mouse;
    Keyboard keyboard;
    Monitor monitor;
    Priter priter;

    //构造方法
    public Compter() {
    }

    public Compter(String name, String id, String productDate, int data) {
        this.name = name;
        this.id = id;
        this.productDate = productDate;
        this.data = data;
    }

    public Compter(String name, String id, String productDate, int data, Mouse mouse, Keyboard keyboard, Monitor monitor, Priter priter) {
        this.name = name;
        this.id = id;
        this.productDate = productDate;
        this.data = data;
        this.mouse = mouse;
        this.keyboard = keyboard;
        this.monitor = monitor;
        this.priter = priter;
    }

    //getting and setting
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getProductDate() {
        return productDate;
    }

    public void setProductDate(String productDate) {
        this.productDate = productDate;
    }

    public int getData() {
        return data;
    }

    public void setData(int data) {
        this.data = data;
    }
}

//接口实现 连接  断开
interface InsertDrawable {
    public void Insert();
    public void Draw();
}

//鼠标类实现接口InsertDrawable
class Mouse implements InsertDrawable {
    public void Insert(){
        System.out.println("鼠标已连接!");
    }
    public void Draw(){
        System.out.println("鼠标已断开!");
    }
}

//键盘类实现接口InsertDrawable
class Keyboard implements InsertDrawable {
    public void Insert(){
        System.out.println("键盘已连接!");
    }
    public void Draw(){
        System.out.println("键盘已断开!");
    }
}

//显示器实现接口InsertDrawable
class Monitor implements InsertDrawable {
    public void Insert(){
        System.out.println("显示器已连接!");
    }
    public void Draw(){
        System.out.println("显示器已断开!");
    }
}

//打印机实现接口InsertDrawable
class Priter implements InsertDrawable {
    public void Insert(){
        System.out.println("打印机已连接!");
    }
    public void Draw(){
        System.out.println("打印机已断开!");
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值