->记录锚点
面向对象编程-继承
- 课堂练习
- 编写Computer类,包含CPU、内存、硬盘等属性,getDeteils方法用于返回Computer的详细信息
- 编写PC子类,继承Computer类,添加特有属性【品牌brand】
- 编写NotePad子类,继承Computer类,添加特有属性【演示color】
- 编写Test类,在main方法中创建PC和NotePad对象,分别给对象中特有的属性赋值,以及从Computer类继承的属性赋值,并使用方法打印输出信息
提示知识点
super关键字
通关源码
Computer类package com.tao.extend.last; public class Computer { private String cpu; private String memory; private String disk; public Computer(String cpu, String memory, String disk) { this.cpu = cpu; this.memory = memory; this.disk = disk; } public String getCpu() { return cpu; } public void setCpu(String cpu) { this.cpu = cpu; } public String getMemory() { return memory; } public void setMemory(String memory) { this.memory = memory; } public String getDisk() { return disk; } public void setDisk(String disk) { this.disk = disk; } void getDetails(){ System.out.println("CPU:" + cpu +"n内存:" + memory + "n硬盘:" + disk); } }
PC类
package com.tao.extend.last; public class PC extends Computer{ private String brand; public PC(String cpu,String memory,String 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 info(){ System.out.println("===PC信息==="); getDetails(); System.out.println("品牌:" + brand); } }
NotePad类
package com.tao.extend.last; public class NotePad extends Computer{ private String color; public NotePad(String cpu,String memory,String 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 info(){ System.out.println("===NotePad信息==="); getDetails(); System.out.println("颜色:" + color); } }
Test测试类
package com.tao.extend.last; public class Test { public static void main(String[] args) { PC pc = new PC("AMD","16G","sata2.0","戴尔G3"); NotePad notePad = new NotePad("AMD","8G","m2","红色"); pc.info(); notePad.info(); } }
->返回
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)