2.21.方法的签名和重载

不同的场景, 不同的购买方法
看代码:不同的购买方法

有重复,但是又明显不一样,怎么办?

方法重载 (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;
        }
    }

}

理解为什么返回值不是方法签名的一部分:不能帮助确定调用哪个方法。

重载的方法可以调用别的重载方法,当然也可以调用别的不重载的方法。
实际上,像这种补充一些缺省的参数值,然后调用重载的方法,是重载的一个重要的使用场景。
重载的方法的方法体内代码可以便写
可以不调用别的重载方法

最后都补充好参数,调用参数最全的一个方法

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值