打字猴:1.70043851e+09
1700438510
1700438511 //简单折扣计算
1700438512
1700438513 public void calPrice(int price, int discount){
1700438514
1700438515 float knockdownPrice=price*discount/100.0F;
1700438516
1700438517 System.out.println(“简单折扣后的价格是:”+formateCurrency(knockdownPrice));
1700438518
1700438519 }
1700438520
1700438521 //复杂多折扣计算
1700438522
1700438523 public void calPrice(int price, int……discounts){
1700438524
1700438525 float knockdownPrice=price;
1700438526
1700438527 for(int discount:discounts){
1700438528
1700438529 knockdownPrice=knockdownPrice*discount/100;
1700438530
1700438531 }
1700438532
1700438533 System.out.println(“复杂折扣后的价格是:”+formateCurrency(knockdownPrice));
1700438534
1700438535 }
1700438536
1700438537 //格式化成本的货币形式
1700438538
1700438539 private String formateCurrency(float price){
1700438540
1700438541 return NumberFormat.getCurrencyInstance().format(price/100);
1700438542
1700438543 }
1700438544
1700438545 public static void main(String[]args){
1700438546
1700438547 Client client=new Client();
1700438548
1700438549 //499元的货物,打75折
1700438550
1700438551 client.calPrice(49900,75);
1700438552
1700438553 }
1700438554
1700438555 }
1700438556
1700438557 这是一个计算商品价格折扣的模拟类,带有两个参数的calPrice方法(该方法的业务逻辑是:提供商品的原价和折扣率,即可获得商品的折扣价)是一个简单的折扣计算方法,该方法在实际项目中经常会用到,这是单一的打折方法。而带有变长参数的calPrice方法则是较复杂的折扣计算方式,多种折扣的叠加运算(模拟类是一种比较简单的实现)在实际生活中也是经常见到的,比如在大甩卖期间对VIP会员再度进行打折;或者当天是你的生日,再给你打个9折,也就是俗话说的“折上折”。
1700438558
1700438559 业务逻辑清楚了,我们来仔细看看这两个方法,它们是重载吗?当然是了,重载的定义是“方法名相同,参数类型或数量不同”,很明显这两个方法是重载。但是再仔细瞧瞧,这个重载有点特殊:calPrice(int price, int……discounts)的参数范畴覆盖了calPrice(int price, int discount)的参数范畴。那问题就出来了:对于calPrice(49900,75)这样的计算,到底该调用哪个方法来处理呢?
[ 上一页 ]  [ :1.70043851e+09 ]  [ 下一页 ]