题目:开放型题目:
设计一个笔记本电脑类,属性随意,并且进行属性私有化,对外提供公开的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("打印机成功接入电脑,正常工作!");
};
}