设计一个笔记本电脑类,属性随意,并且进行属性私有化,对外提供公开的set和get方法。 设计一个可插拔的接口:InsertDrawable,该接口有什么方法自行定义。
生活随笔
收集整理的這篇文章主要介紹了
设计一个笔记本电脑类,属性随意,并且进行属性私有化,对外提供公开的set和get方法。 设计一个可插拔的接口:InsertDrawable,该接口有什么方法自行定义。
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
代碼
/* 開(kāi)放型題目:設(shè)計(jì)一個(gè)筆記本電腦類,屬性隨意,并且進(jìn)行屬性私有化,對(duì)外提供公開(kāi)的set和get方法。設(shè)計(jì)一個(gè)可插拔的接口:InsertDrawable,該接口有什么方法自行定義。設(shè)計(jì)一個(gè)鼠標(biāo)類,實(shí)現(xiàn)InsertDrawable接口,并實(shí)現(xiàn)方法。設(shè)計(jì)一個(gè)鍵盤類,實(shí)現(xiàn)InsertDrawable接口,并實(shí)現(xiàn)方法。設(shè)計(jì)一個(gè)顯示器類,實(shí)現(xiàn)InsertDrawable接口,并實(shí)現(xiàn)方法。設(shè)計(jì)一個(gè)打印機(jī)類,實(shí)現(xiàn)InsertDrawable接口,并實(shí)現(xiàn)方法。在“筆記本電腦類”中有一個(gè)InsertDrawable接口屬性,可以讓筆記本電腦可插拔鼠標(biāo)、鍵盤、顯示器、打印機(jī)等。編寫測(cè)試程序,創(chuàng)建多個(gè)對(duì)象,演示接口的作用。 */public class Homework1 {public static void main(String[] args) {//創(chuàng)建鼠標(biāo)、鍵盤、顯示器、打印機(jī)對(duì)象InsertDrawable mouse = new Mouse();InsertDrawable keyboard = new Keyboard();InsertDrawable display = new Display();InsertDrawable printer = new Printer();//創(chuàng)建電腦對(duì)象Computer c = new Computer();//將鼠標(biāo)、鍵盤、顯示器、打印機(jī)插入電腦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("鼠標(biāo)已接入,正在工作!");} }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("打印機(jī)已接入,正在工作!");} }class Computer{//電腦有n個(gè)插槽private InsertDrawable slot[];//constructorpublic Computer(InsertDrawable[] slot) {this.slot = slot;}public Computer() {//默認(rèn)有5個(gè)插槽this(new InsertDrawable[5]);}//setter and getterpublic InsertDrawable[] getSlot() {return slot;}public void setSlot(InsertDrawable[] slot) {this.slot = slot;} }總結(jié)
以上是生活随笔為你收集整理的设计一个笔记本电脑类,属性随意,并且进行属性私有化,对外提供公开的set和get方法。 设计一个可插拔的接口:InsertDrawable,该接口有什么方法自行定义。的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: linux 流函数,标准IO函数库 -
- 下一篇: Dos命令使用Latex编写“Hello