Java 上機測驗2020.4.29

1、定義一個Computer類,要求包含以下屬性和方法:
屬性:型號(model),操作系統(OS)
包含2個構造方法
2個參數(String model,String OS)通過參數傳遞設置屬性的值。
1個參數(String model)通過參數設置屬性model的值,並把OS的值設置爲”Windows10”
成員方法
定義所有屬性的getter、setter訪問器;
定義方法work( ):輸出“電腦都擁有操作系統,可運行程序!”
定義方法print( ):輸出品牌、操作系統等信息。

2、定義Computer的子類Laptop(筆記本電腦):
分別定義包含2個參數和1個參數的構造方法。
覆蓋父類的work( )方法:輸出:“手提電腦,便於攜帶,用於移動辦公環境!”

3、定義Computer的子類IPad:
定義1個參數的構造方法(把OS的值設置爲默認值“iPadOS13”)。
覆蓋父類的work( )方法:輸出:“觸屏操作,輕便,用於娛樂消遣!”

4、定義測試類ComputerDemo,編寫main方法,在main方法裏面分別創建Computer、Laptop、IPad類的對象,並分別調用他們的work、print方法。

代碼

package game;

public class Java上機作業429{
    public static void main(String[] args) {
	Computer computer = new Computer("Microsoft");
	Laptop laptop = new Laptop("Microsoft");
	IPad iPad = new IPad("Microsoft");
	computer.work();
	computer.print();
	laptop.work();
	laptop.print();
	iPad.work();
	iPad.print();
    }
}
class Computer{
    private String model,OS;

    public Computer(String model, String oS) { this.model = model; this.OS = oS; }
    public Computer(String model) { this.model = model; this.OS = "Windows10"; }
	
    public String getModel() { return model; }
    public void setModel(String model) { this.model = model; }
	
    public String getOS() { return OS; }
    public void setOS(String oS) { OS = oS; }
	
    public void work() { System.out.println("電腦都擁有操作系統,可運行程序!"); }
	
    public void print() {
	System.out.println("品牌:"+this.model);
	System.out.println("操作系統:"+this.OS);
    }	
}

class Laptop extends Computer{
    public Laptop(String model) { super(model); }
    public Laptop(String model, String oS) { super(model, oS); }
	
    public void work() { System.out.println("手提電腦,便於攜帶,用於移動辦公環境!"); }
}

class IPad extends Computer{
    public IPad(String model) { super(model,"iPadOS13"); }
	
    public void work() { System.out.println("觸屏操作,輕便,用於娛樂消遣!"); }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章