1 public abstract class Card { 2 private String name; 3 private double money; 4 5 public abstract void pay(double money); 6 7 public String getName() { 8 return name; 9 } 10 11 public void setName(String name) { 12 this.name = name; 13 } 14 15 public double getMoney() { 16 return money; 17 } 18 19 public void setMoney(double money) { 20 this.money = money; 21 } 22 }
1 public class GoldCard extends Card{ 2 @Override 3 public void pay(double money) { 4 // 优惠后金额 5 double rs = money * 0.8; 6 double lastMoney = getMoney() - rs; 7 8 System.out.println(getName() + "当前账户金额:" + getMoney() 9 + "当前消费:" + rs + "元,当前剩余:" + lastMoney + "元" 10 ); 11 12 setMoney(lastMoney); 13 } 14 }
1 public class Test { 2 public static void main(String[] args) { 3 GoldCard c = new GoldCard(); 4 c.setMoney(10000.0); 5 c.setName("张三"); 6 c.pay(300); 7 } 8 }
标签:name,示例,double,void,money,抽象类,public,String From: https://www.cnblogs.com/dsds/p/17980044