设计一个笔记本电脑类,属性随意,并且进行属性私有化,对外提供公开的set和get方法。 设计一个可插拔的接口:InsertDrawable,该接口有什么方法自行定义。
生活随笔
收集整理的這篇文章主要介紹了
设计一个笔记本电脑类,属性随意,并且进行属性私有化,对外提供公开的set和get方法。 设计一个可插拔的接口:InsertDrawable,该接口有什么方法自行定义。
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
代碼
/* 開放型題目:設計一個筆記本電腦類,屬性隨意,并且進行屬性私有化,對外提供公開的set和get方法。設計一個可插拔的接口:InsertDrawable,該接口有什么方法自行定義。設計一個鼠標類,實現InsertDrawable接口,并實現方法。設計一個鍵盤類,實現InsertDrawable接口,并實現方法。設計一個顯示器類,實現InsertDrawable接口,并實現方法。設計一個打印機類,實現InsertDrawable接口,并實現方法。在“筆記本電腦類”中有一個InsertDrawable接口屬性,可以讓筆記本電腦可插拔鼠標、鍵盤、顯示器、打印機等。編寫測試程序,創建多個對象,演示接口的作用。 */public class Homework1 {public static void main(String[] args) {//創建鼠標、鍵盤、顯示器、打印機對象InsertDrawable mouse = new Mouse();InsertDrawable keyboard = new Keyboard();InsertDrawable display = new Display();InsertDrawable printer = new Printer();//創建電腦對象Computer c = new Computer();//將鼠標、鍵盤、顯示器、打印機插入電腦c.getSlot()[0] = mouse;c.getSlot()[1] = keyboard;c.getSlot()[2] = display;c.getSlot()[3] = printer;//工作c.getSlot()[0].work();c.getSlot()[1].work();c.getSlot()[2].work();c.getSlot()[3].work();} }interface InsertDrawable{//工作類void work(); }class Mouse implements InsertDrawable{@Overridepublic void work() {System.out.println("鼠標已接入,正在工作!");} }class Keyboard implements InsertDrawable{@Overridepublic void work() {System.out.println("鍵盤已接入,正在工作!");} }class Display implements InsertDrawable{@Overridepublic void work() {System.out.println("顯示器已接入,正在工作!");} }class Printer implements InsertDrawable{@Overridepublic void work() {System.out.println("打印機已接入,正在工作!");} }class Computer{//電腦有n個插槽private InsertDrawable slot[];//constructorpublic Computer(InsertDrawable[] slot) {this.slot = slot;}public Computer() {//默認有5個插槽this(new InsertDrawable[5]);}//setter and getterpublic InsertDrawable[] getSlot() {return slot;}public void setSlot(InsertDrawable[] slot) {this.slot = slot;} }總結
以上是生活随笔為你收集整理的设计一个笔记本电脑类,属性随意,并且进行属性私有化,对外提供公开的set和get方法。 设计一个可插拔的接口:InsertDrawable,该接口有什么方法自行定义。的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux 流函数,标准IO函数库 -
- 下一篇: killall 后面信号_Linux k