看例程:学习 super 的使用方法
public class Phone extends MerchandiseV2 {
// 给Phone增加新的属性和方法
private double screenSize;
private double cpuHZ;
private int memoryG;
private int storageG;
private String brand;
private String os;
private static int MAX_BUY_ONE_ORDER = 5;
public Phone(
String name, String id, int count, double soldPrice, double purchasePrice,
double screenSize, double cpuHZ, int memoryG, int storageG, String brand, String os
) {
// >> TODO 使用super调用父类的构造方法,必须是子类构造方法的第一个语句
// >> TODO 可以使用表达式
// >> TODO super调用构造方法,不可以使用super访问父类的属性和方法,不可以使用子类成员变量和方法
// >> TODO 可以使用静态变量和方法
// >> TODO 都是<init>方法,我们来看一下
super(name, id, count, soldPrice * 1.2, purchasePrice);
init(screenSize, cpuHZ, memoryG, storageG, brand, os);
}
public Phone() {
super("无名", "000", 0, 1, 1.1);
init(4.5, 4.6, 6, 128, "Uknown", "Uknown");
}
public void init(double screenSize, double cpuHZ, int memoryG, int storageG, String brand, String os) {
this.screenSize = screenSize;
this.cpuHZ = cpuHZ;
this.memoryG = memoryG;
this.storageG = storageG;
this.brand = brand;
this.os = os;
}
public double buy(int count) {
if (count > MAX_BUY_ONE_ORDER) {
System.out.println("购买失败,手机一次最多只能买" + MAX_BUY_ONE_ORDER + "个");
return -2;
}
return super.buy(count);
}
public String getName() {
return this.brand + ":" + this.os + ":" + super.getName();
}
public void describe() {
System.out.println("此手机商品属性如下");
super.describe();
System.out.println("手机厂商为" + brand + ";系统为" + os + ";硬件配置如下:\n" +
"屏幕:" + screenSize + "寸\n" +
"cpu主频" + cpuHZ + " GHz\n" +
"内存" + memoryG + "Gb\n" +
"存储空间" + storageG + "Gb");
}
public boolean meetCondition() {
return true;
}
public double getScreenSize() {
return screenSize;
}
public void setScreenSize(double screenSize) {
this.screenSize = screenSize;
}
public double getCpuHZ() {
return cpuHZ;
}
public void setCpuHZ(double cpuHZ) {
this.cpuHZ = cpuHZ;
}
public int getMemoryG() {
return memoryG;
}
public void setMemoryG(int memoryG) {
this.memoryG = memoryG;
}
public int getStorageG() {
return storageG;
}
public void setStorageG(int storageG) {
this.storageG = storageG;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getOs() {
return os;
}
public void setOs(String os) {
this.os = os;
}
}
使用super调用父类的构造方法,必须是子类构造方法的第一个语句
可以使用表达式
super调用构造方法,不可以使用super访问父类的属性和方法,不可以使用子类成员变量和方法
可以使用静态变量和方法
都是方法