不同的场景, 不同的购买方法
看代码:不同的购买方法
有重复,但是又明显不一样,怎么办?
方法重载 (overload)
方法签名:方法名+依次参数类型。注意,返回值不属于方法签名。方法签名是一个方法在一个类中的唯一标识
同一个类中方法可以重名,但是签名不可以重复。一个类中如果定义了名字相同,签名不同的方法,就叫做方法的重载
看代码:重写我们的购买方法,理解方法签名
public class MerchandiseV2OverrideBuyAppMain {
public static void main(String[] args) {
MerchandiseV2Overload merchandise = new MerchandiseV2Overload();
merchandise.init("书桌", "DESK9527", 40, 999.9, 500);
// >> TODO 理解为什么返回值不是方法签名的一部分:不能帮助确定调用哪个方法。
merchandise.buy();
merchandise.describe();
double cost = merchandise.buy(10);
System.out.println(cost);
merchandise.describe();
double costVIP = merchandise.buy(10, true);
System.out.println(costVIP);
merchandise.describe();
}
}
public class MerchandiseV2 {
public String name;
public String id;
public int count;
public double soldPrice;
public double purchasePrice;
public void init(String name, String id, int count, double soldPrice, double purchasePrice) {
this.name = name;
this.id = id;
this.count = count;
this.soldPrice = soldPrice;
this.purchasePrice = purchasePrice;
}
public void describe() {
System.out.println("商品名字叫做" + name + ",id是" + id + "。 商品售价是" + soldPrice
+ "。商品进价是" + purchasePrice + "。商品库存量是" + count +
"。销售一个的毛利润是" + (soldPrice - purchasePrice));
}
public double calculateProfit() {
double profit = soldPrice - purchasePrice;
// if(profit <= 0){
// return 0;
// }
return profit;
}
// TODO 为了使用方便,买一个,不传参数
public double buyOne() {
int count = 1;
if (this.count < count) {
return -1;
}
this.count--;
return count * soldPrice;
}
// TODO 买好几个
public double buyCount(int count) {
if (this.count < count) {
return -1;
}
this.count -= count;
return count * soldPrice;
}
// TODO VIP会员 购买九五折
public double buyAsVIP(int count, boolean isVIP) {
if (this.count < count) {
return -1;
}
this.count -= count;
double totalCost = count * soldPrice;
if (isVIP) {
return totalCost * 0.95;
} else {
return totalCost;
}
}
}
public class MerchandiseV2Overload {
public String name;
public String id;
public int count;
public double soldPrice;
public double purchasePrice;
public void init(String name, String id, int count, double soldPrice, double purchasePrice) {
this.name = name;
this.id = id;
this.count = count;
this.soldPrice = soldPrice;
this.purchasePrice = purchasePrice;
}
public void describe() {
System.out.println("商品名字叫做" + name + ",id是" + id + "。 商品售价是" + soldPrice
+ "。商品进价是" + purchasePrice + "。商品库存量是" + count +
"。销售一个的毛利润是" + (soldPrice - purchasePrice));
}
public double calculateProfit() {
double profit = soldPrice - purchasePrice;
// if(profit <= 0){
// return 0;
// }
return profit;
}
// >> TODO 重载的方法可以调用别的重载方法,当然也可以调用别的不重载的方法。
// >> TODO 实际上,像这种补充一些缺省的参数值,然后调用重载的方法,是重载的一个重要的使用场景。
// >> TODO 在这里我们举的例子就是这样的,但是不是语法要求一定要这样。重载的方法的方法体内代码可以随便写,
// TODO 可以不调用别的重载方法
public double buy() {
return buy(1);
}
public double buy(int count) { return buy(count, false);
}
// TODO 最后都补充好参数,调用参数最全的一个方法
public double buy(int count, boolean isVIP) {
if (this.count < count) {
return -1;
}
this.count -= count;
double totalCost = count * soldPrice;
if (isVIP) {
return totalCost * 0.95;
} else {
return totalCost;
}
}
}
理解为什么返回值不是方法签名的一部分:不能帮助确定调用哪个方法。
重载的方法可以调用别的重载方法,当然也可以调用别的不重载的方法。
实际上,像这种补充一些缺省的参数值,然后调用重载的方法,是重载的一个重要的使用场景。
重载的方法的方法体内代码可以便写
可以不调用别的重载方法
最后都补充好参数,调用参数最全的一个方法