Java面向抽象编程-接口

题目描述

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

实现效果

拔插鼠标!

Java代码

public class Homework2101 {
    public static void main(String[] args) {
        Mouse m = new Mouse();
        Computer c = new Computer("惠普",1.5,m);
        c.printInfo();
    }
}
class Computer{
    //电脑品牌
    private String brand;
    //电脑重量
    private double weight;
    //接口属性
    private InsertDrawable i;

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    public InsertDrawable getI() {
        return i;
    }

    public void setI(InsertDrawable i) {
        this.i = i;
    }

    public Computer() {
    }

    public Computer(String brand, double weight, InsertDrawable i) {
        this.brand = brand;
        this.weight = weight;
        this.i = i;
    }
    public void printInfo(){
        i.plug();
    }
}
//可拔插的接口
interface InsertDrawable{
    void plug();
}
//鼠标类
class Mouse implements  InsertDrawable{
    public void plug(){
        System.out.println("拔插鼠标!");
    }
}
//键盘类
class Keyboard implements InsertDrawable{
    public void plug(){
        System.out.println("拔插键盘!");
    }
}
//显示器类
class Monitor implements InsertDrawable{
    public void plug(){
        System.out.println("拔插显示器!");
    }
}
//打印机类
class Printer implements InsertDrawable{
    public void plug(){
        System.out.println("拔插打印机!");
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值