public class Computer {
private double cpu;
private int memory;
private int disk;
private double price;
public Computer() {}
public Computer(double cpu, int memory, int disk, double price) {
this.cpu = cpu;
this.memory = memory;
this.disk = disk;
this.price = price;
}
public void setCpu(double cpu) {
this.cpu = cpu;
}
public double getCpu() {
return cpu;
}
public void setMemory(int memory) {
this.memory = memory;
}
public int getMemory() {
return memory;
}
public void setDisk(int disk) {
this.disk = disk;
}
public int getDisk() {
return disk;
}
public void setPrice(double price) {
this.price = price;
}
public double getPrice() {
return price;
}
public String getDetails() {
return "CPU:" + cpu + ",内存:" + memory + ",硬盘:" + disk + ",价格:" + price;
}
}
class PC extends Computer {
private String keyboard;
public PC() {
}
public PC(double cpu, int memory, int disk, double price, String keyboard) {
super(cpu, memory, disk, price); // 直接显式调用
this.keyboard = keyboard;
}
public void setKeyboard(String keyboard) {
this.keyboard = keyboard;
}
public String getKeyboard() {
return keyboard;
}
public void code() {
System.out.println("使用PC开发程序.内存是:" + getMemory());
}
@Override
public String getDetails() {
return super.getDetails() + ",键盘:" + keyboard;
}
}
class NotePad extends Computer {
private int satelite;
public NotePad() {}
public NotePad(double cpu, int memory, int disk, double price, int satelite) {
super(cpu, memory, disk, price);
this.satelite = satelite;
}
public void setSatelite(int satelite) {
this.satelite = satelite;
}
public int getSatelite() {
return satelite;
}
public void navigate() {
System.out.println("使用平板在导航, 使用的CPU:" + getCpu());
}
@Override
public String getDetails() {
return super.getDetails() + ",卫星:" + satelite;
}
}
class ComputerTest2 {
/*
在Test类中提供一个静态方法listPrice,以Computer引用变量为参数,打印输出电脑价格。
在main方法中,分别以Computer、PC、NotePad对象为参数,调用listPrice方法。
在方法listPrice中,判断Computer参数的真实对象,并调用不同对象上的特有方法
*/
public static void listPrice(Computer com) {
System.out.println(com.getPrice());
if (com instanceof PC) {
PC p = (PC)com;
p.code();
} else if (com instanceof NotePad) {
((NotePad)com).navigate();
}
}
public static void main(String[] args) {
PC pc = new PC(4.0, 16, 5000, 4000, "DELL键盘");
NotePad np = new NotePad(2.0, 1, 32, 2000, 6);
Computer com = new Computer(2.5, 2, 500, 1500);
listPrice(pc);
listPrice(np);
listPrice(com);
}
}
class ComputerTest {
public static void main(String[] args) {
Computer[] coms = new Computer[6];
coms[0] = new PC(4.0, 16, 5000, 4000, "DELL键盘");
coms[1] = new NotePad(2.0, 1, 32, 2000, 6);
coms[2] = new NotePad(3.0, 4, 128, 5000, 12);
coms[3] = new Computer(2.5, 2, 500, 1500);
coms[4] = new PC(5.0, 32, 30000, 20000, "苹果键盘");
coms[5] = new Computer(1.0, 1, 200, 800);
for (int i = 0; i < coms.length; i++) {
System.out.println(coms[i].getDetails());
}
System.out.println("---------------------------");
for (int i = 0; i < coms.length - 1; i++) {
for (int j = 0; j < coms.length - 1 - i; j++) {
if (coms[j].getPrice() > coms[j + 1].getPrice()) {
Computer tmp = coms[j];
coms[j] = coms[j + 1];
coms[j + 1] = tmp;
}
}
}
for (Computer com : coms) {
System.out.println(com.getDetails());
}
}
}
java学习之路 之 高级类特性1-多态、重写-练习题2
最新推荐文章于 2021-07-20 17:41:30 发布