编写Computer类包含CPU、内存、硬盘等属性,getDetails方法用于返回Computer的详细信息
编写pc子类,继承Computer类,添加特有属性{品牌brand
编写Notepad子类,继承Computer类,添加特有属性{颜色color
编写Test类,在main方法中创建PC和Notepad对象,分别给对象中特有的属性赋值
以及从computer类继承属性赋值,并使用方法并打印输出
父类Computer
package com.hspedu.extend.exercise; //Computer类包含CPU、内存、硬盘等属性, // getDetails方法用于返回Computer的详细信息 public class Computer { private String cpu; private int memory; private int disk; public Computer(String cpu, int memory, int disk) { this.cpu = cpu; this.memory = memory; this.disk = disk; } //返回详细信息 public String getDetails() { return "cpu=" + cpu + "memory=" + memory + "disk=" + disk; } public String getCpu() { return cpu; } public void setCpu(String cpu) { this.cpu = cpu; } public int getMemory() { return memory; } public void setMemory(int memory) { this.memory = memory; } public int getDisk() { return disk; } public void setDisk(int disk) { this.disk = disk; } }
子类PC
package com.hspedu.extend.exercise; public class PC extends Computer{ private String brand; public PC(String cpu, int memory, int disk, String brand) { super(cpu, memory, disk); this.brand = brand; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public void printInfo(){ System.out.println("pc信息:"+getDetails() + "brands=" + brand); } }
子类Notepad
package com.hspedu.extend.exercise; public class Notepad extends Computer{ private String color; public Notepad(String cpu, int memory, int disk, String color) { super(cpu, memory, disk); this.color = color; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public void printInfo(){ System.out.println("NotePad信息:"+getDetails() + "color=" + color); } }
主类Test(Exercies03)
package com.hspedu.extend.exercise; public class Exercies03 { public static void main(String[] args) { PC pc = new PC("intel",8,500,"AMB"); pc.printInfo(); Notepad notepad = new Notepad("麒麟",16,1000,"red"); notepad.printInfo(); } }
运行结果
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)