JavaCard
⑴ java定义一个银行卡类(BankCard)以及派生类ForeignCard,LimitedCard,成员包括姓名,卡
abstract class BankCard {
protected String name;
protected String cardNo;
protected double money;
public BankCard(String name, String cardNo, double money) {
super();
this.name = name;
this.cardNo = cardNo;
this.money = money;
}
public String getName() {
return name;
}
public String getCardNo() {
return cardNo;
}
public double getMoney() {
return money;
}
public abstract void unputMoney(int someMoney);
public void putMoney(int someMoney) {
this.money += someMoney;
}
}
class ForeignCard extends BankCard {
public ForeignCard(String name, String cardNo, double money) {
super(name, cardNo, money);
}
@Override
public void unputMoney(int someMoney) {
double left = getMoney() - someMoney * 1.01;
if(left >= 0) {
super.money = left;
} else {
throw new RuntimeException("余额不足");
}
}
}
class LimitedCard extends BankCard {
public LimitedCard(String name, String cardNo, double money) {
super(name, cardNo, money);
}
@Override
public void unputMoney(int someMoney) {
if(someMoney > 1000) {
throw new RuntimeException("取款不能超过1000");
}
if(getMoney() < someMoney) {
throw new RuntimeException("余额不足");
}
super.money -= someMoney;
}
}