java学习之路 之 高级类特性1-多态、重写-练习题2

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());
		}
		
	}
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值