2021年第三次blog总结
一、前言
经过第最后一段时间的学习,已经了进一步了解到面向对象的程序设计的一些特点与方法。下面将总结三次习题集的一些要点。
1.习题集七 总共2题:
两题都考查类的继承、多态性使用方法以及接口的应用。
2.习题集八 总共1题:
该题为ATM机的仿生设计,此次仅涉及普通的借记卡较为简单
3.习题集九 总共1题:
该次为上一次的迭代,增加了信用卡。
二、设计与分析
1.卡片游戏

首先,定义一个抽象类为shape为定下基本的求面积的方法,在其后面的各个实体子类根据不同形状来Override

由于题目是以卡片作为各个图形的载体,在设计一个一个Card类来承载这些shape,最后一个DealCardList类来处理这些卡片
在第二次迭代中需要将各个类型形状分类别来排序
1 public void SeparatedList(ArrayList<Integer> List) { 2 3 for(int i =0;i<List.size();i++) { 4 5 switch(List.get(i)) { 6 case 1:{ 7 cardList1.add(cardList.get(i)); 8 flag1 = true; 9 break; 10 } 11 case 2:{ 12 cardList2.add(cardList.get(i)); 13 flag2 = true; 14 break; 15 } 16 case 3:{ 17 cardList3.add(cardList.get(i)); 18 flag3 = true; 19 break; 20 } 21 case 4:{ 22 cardList4.add(cardList.get(i)); 23 flag4 = true; 24 break; 25 } 26 } 27 } 28 }
在DealCardList中写了一个SeparatedList方法,通过switch语句将cardlist中的形状通过输入的数字进行分类。


二者复杂度对比在这里呈现,前者为8,后者为13
2.ATM仿生设计

该题初始数据,红色字体为第二次迭代要求。

输入输出样式在这里,第二次主要是增加透支的功能。
首先分析无透支情况下的:

简单来说,银联管理多家银行,银行管理多个账户与多个ATM机,用户持有多个账户,账户下对应一张或多张卡,而用户的钱存放在账户中
接下来具体分析我的代码
1 class Bank { 2 ArrayList<ATM> ATMList = new ArrayList<ATM>(); 3 ArrayList<Account> AccountList = new ArrayList<Account>(); 4 5 public Bank() { 6 } 7 8 public Bank(ArrayList<ATM> ATMList, ArrayList<Account> accountList) { 9 this.ATMList = ATMList; 10 AccountList = accountList; 11 } 12 13 public ArrayList<Account> getAccountList() { 14 return AccountList; 15 } 16 17 public void setAccountList(ArrayList<Account> accountList) { 18 AccountList = accountList; 19 } 20 21 public ArrayList<ATM> getATMList() { 22 return ATMList; 23 } 24 25 public void setATMList(ArrayList<ATM> ATMList) { 26 this.ATMList = ATMList; 27 } 28 29 }
Bank中可以对ATM和Account进行增删
1 class Account { 2 private double blance=10000; 3 private String NAME; 4 private String accountNumber; 5 private ArrayList<Card> CardList = new ArrayList<Card>(); 6 private ArrayList<String> cardNumber; 7 8 public ArrayList<Card> getCardList() { 9 return CardList; 10 } 11 12 public void setCardList() { 13 for(int i = 0; i < cardNumber.size(); i++){ 14 Card temp = new Card(); 15 temp.setNumber(cardNumber.get(i)); 16 CardList.add(temp); 17 } 18 } 19 20 public ArrayList<String> getCardNumber() { 21 return cardNumber; 22 } 23 24 public void setCardNumber(ArrayList<String> cardNumber) { 25 this.cardNumber = cardNumber; 26 } 27 28 public double getBlance() { 29 return blance; 30 } 31 32 public void setBlance(double blance) { 33 this.blance = blance; 34 } 35 36 public String getNAME() { 37 return NAME; 38 } 39 40 public void setNAME(String NAME) { 41 this.NAME = NAME; 42 } 43 44 public String getAccountNumber() { 45 return accountNumber; 46 } 47 48 public void setAccountNumber(String accountNumber) { 49 this.accountNumber = accountNumber; 50 } 51 52 53 54 public Account(double blance, String NAME, String accountNumber, String password) { 55 this.blance = blance; 56 this.NAME = NAME; 57 this.accountNumber = accountNumber; 58 } 59 60 public Account() { 61 } 62 63 public void changeBalance(double need){ 64 setBlance(blance-need); 65 } 66 67 }
Account实现对账户的卡的增删及余额,账号,持有者姓名的更改。
1 class Card { 2 private String Number; 3 private String password = "88888888"; 4 5 public String getPassword() { 6 return password; 7 } 8 9 public void setPassword(String password) { 10 this.password = password; 11 } 12 13 public Card(String number) { 14 Number = number; 15 } 16 17 public Card() { 18 } 19 20 public boolean cardMatcher(String password) { 21 if(password.equals(getPassword())){ 22 return true; 23 } 24 return false; 25 26 } 27 public String getNumber() { 28 return Number; 29 } 30 31 public void setNumber(String number) { 32 Number = number; 33 } 34 }
Card除了对卡号及密码的更改,还设计了密码的匹配(这个方法不应该放在这里)
1 class User { 2 private String Name; 3 private ArrayList<Account> Accounts= new ArrayList<Account>(); 4 public ArrayList<Account> getAccounts() { 5 return Accounts; 6 } 7 8 public void setAccounts(ArrayList<Account> accounts) { 9 Accounts = accounts; 10 } 11 12 13 14 public User(String name) { 15 Name = name; 16 } 17 18 public User() { 19 } 20 21 public String getName() { 22 return Name; 23 } 24 25 public void setName(String name) { 26 Name = name; 27 } 28 public void createAccount(String AccountNumber,String NAME,ArrayList<String> CardNumber) { 29 Account temp = new Account(); 30 temp.setAccountNumber(AccountNumber); 31 temp.setNAME(NAME); 32 temp.setCardNumber(CardNumber); 33 temp.setCardList(); 34 Accounts.add(temp); 35 } 36 }
User除了常规的,设计了一个开户的方法用于初始化数据。
剩下的输入处理,和后续操作都被我放在主类中(不好)
1 public class Main { 2 public static void main(String[] args) { 3 ChinaUnionPay chinaUnionPay = new ChinaUnionPay(); 4 //ATM机的初始化 5 int i = 1; 6 ArrayList<ATM> ATMs1 = new ArrayList<ATM>(); 7 while(i<=4){ 8 ATM temp = new ATM("0"+i); 9 ATMs1.add(temp); 10 i++; 11 } 12 chinaUnionPay.CCB.setATMList(ATMs1); 13 14 ArrayList<ATM> ATMs2 = new ArrayList<ATM>(); 15 while(i<=6){ 16 ATM temp = new ATM("0"+i); 17 ATMs2.add(temp); 18 i++; 19 } 20 chinaUnionPay.ICBC.setATMList(ATMs2); 21 //User的初始化 22 User YangGuo = new User("杨过"); 23 User GuoJing = new User("郭靖"); 24 User ZhangWuJi = new User("张无忌"); 25 User WeiXiaoBao = new User("韦小宝 "); 26 27 ArrayList<String> temp = new ArrayList<String>(); 28 29 temp.add("6217000010041315709"); 30 temp.add("6217000010041315715"); 31 YangGuo.createAccount("3217000010041315709","杨过" ,temp); 32 temp = new ArrayList<String>(); 33 34 temp.add("6217000010041315718"); 35 YangGuo.createAccount("3217000010041315715","杨过" ,temp); 36 temp = new ArrayList<String>(); 37 38 temp.add("6217000010051320007"); 39 GuoJing.createAccount("3217000010051320007","郭靖" ,temp); 40 temp = new ArrayList<String>(); 41 42 temp.add("6222081502001312389"); 43 ZhangWuJi.createAccount("3222081502001312389","张无忌" ,temp); 44 temp = new ArrayList<String>(); 45 46 temp.add("6222081502001312390"); 47 ZhangWuJi.createAccount("3222081502001312390","张无忌" ,temp); 48 temp = new ArrayList<String>(); 49 50 temp.add("6222081502001312399"); 51 temp.add("6222081502001312400"); 52 ZhangWuJi.createAccount("3222081502001312399","张无忌" ,temp); 53 temp = new ArrayList<String>(); 54 55 temp.add("6222081502051320785"); 56 WeiXiaoBao.createAccount("3222081502051320785","韦小宝" ,temp); 57 temp = new ArrayList<String>(); 58 59 temp.add("6222081502051320786"); 60 WeiXiaoBao.createAccount("3222081502051320786","韦小宝" ,temp); 61 temp = new ArrayList<String>(); 62 63 //将User开户信息导入Bank 64 ArrayList<Account> CCBAccountList = new ArrayList<Account>(); 65 CCBAccountList.addAll(YangGuo.getAccounts()); 66 CCBAccountList.addAll(GuoJing.getAccounts()); 67 chinaUnionPay.CCB.setAccountList(CCBAccountList); 68 69 ArrayList<Account> ICBCAccountList = new ArrayList<Account>(); 70 ICBCAccountList.addAll(ZhangWuJi.getAccounts()); 71 ICBCAccountList.addAll(WeiXiaoBao.getAccounts()); 72 chinaUnionPay.ICBC.setAccountList(ICBCAccountList); 73 74 //处理输入数据 75 Scanner sc = new Scanner(System.in); 76 ArrayList<String> in = new ArrayList<String>(); 77 String value = sc.nextLine(); 78 in.add(value); 79 while (value.equals("#")==false){ 80 value = sc.nextLine(); 81 in.add(value); 82 } 83 int a = 0; 84 ArrayList<String> CardNumber = new ArrayList<String>(); 85 ArrayList<String> Password = new ArrayList<String>(); 86 ArrayList<String> ATMNumber = new ArrayList<String>(); 87 ArrayList<Double> need = new ArrayList<Double>(); 88 Pattern s = Pattern.compile("\\d+ +\\d+ +\\d+ +[+-]?\\d+.\\d\\d"); 89 Pattern ss = Pattern.compile("\\d+"); 90 while (in.get(a).equals("#")==false){ 91 Matcher x = s.matcher(in.get(a)); 92 Matcher xx = ss.matcher(in.get(a)); 93 boolean y = x.matches(); // 返回 true 94 boolean yy = xx.matches(); // 返回 true 95 if (y) { 96 String split[] = in.get(a).split(" +"); 97 CardNumber.add(split[0]); 98 Password.add(split[1]); 99 ATMNumber.add(split[2]); 100 need.add(Double.parseDouble(split[3])); 101 } 102 if(yy){ 103 CardNumber.add(in.get(a)); 104 Password.add(""); 105 ATMNumber.add("01"); 106 need.add(0.00); 107 } 108 a++; 109 } 110 111 //ATM机校验 112 Pattern p = Pattern.compile("0[1-6]"); 113 int b = 0; 114 while (b<ATMNumber.size()){ 115 Matcher m = p.matcher(ATMNumber.get(b)); 116 boolean n = m.matches(); // 返回 true 117 if(n==false) { 118 System.out.println("Sorry,the ATM's id is wrong."); 119 System.exit(0); 120 } 121 b++; 122 } 123 124 //卡号与密码与现金校验 125 int c = 0; 126 while (c<CardNumber.size()){ 127 boolean flagN = false;//卡号 128 boolean flagP = false;//密码 129 boolean flagB = false;//余额 130 boolean flagX = false;//跨行 131 Pattern ccb = Pattern.compile("0[1-4]"); 132 Pattern icbc = Pattern.compile("0[5-6]"); 133 for (int j = 0; j < chinaUnionPay.CCB.getAccountList().size(); j++) { 134 for (int jj = 0; jj < chinaUnionPay.CCB.getAccountList().get(j).getCardNumber().size(); jj++) { 135 if(chinaUnionPay.CCB.getAccountList().get(j).getCardNumber().get(jj).equals(CardNumber.get(c))){ 136 if(Password.get(c).equals("")){ 137 flagP=true; 138 flagX=true; 139 } 140 else { 141 flagP = chinaUnionPay.CCB.getAccountList().get(j).getCardList().get(jj).cardMatcher(Password.get(c)); 142 Matcher m = ccb.matcher(ATMNumber.get(c)); 143 flagX = m.matches(); // 返回 true 144 } 145 flagB=chinaUnionPay.CCB.getAccountList().get(j).getBlance()>= need.get(c); 146 flagN = true; 147 } 148 149 } 150 } 151 for (int ii = 0; ii < chinaUnionPay.ICBC.getAccountList().size(); ii++) { 152 for (int iii = 0; iii < chinaUnionPay.ICBC.getAccountList().get(ii).getCardNumber().size(); iii++) { 153 if(chinaUnionPay.ICBC.getAccountList().get(ii).getCardNumber().get(iii).equals(CardNumber.get(c))){ 154 if(Password.get(c).equals("")){ 155 flagP=true; 156 flagX=true; 157 } 158 else { 159 flagP = chinaUnionPay.ICBC.getAccountList().get(ii).getCardList().get(iii).cardMatcher(Password.get(c)); 160 Matcher m = icbc.matcher(ATMNumber.get(c)); 161 flagX = m.matches(); // 返回 true 162 } 163 flagB=chinaUnionPay.ICBC.getAccountList().get(ii).getBlance()>= need.get(c); 164 flagN = true; 165 } 166 167 } 168 } 169 if (flagN == false) { 170 System.out.println("Sorry,this card does not exist."); 171 System.exit(0); 172 } 173 if (flagP == false) { 174 System.out.println("Sorry,your password is wrong."); 175 System.exit(0); 176 } 177 if (flagB == false) { 178 System.out.println("Sorry,your account balance is insufficient."); 179 System.exit(0); 180 } 181 if (flagX == false) { 182 System.out.println("Sorry,cross-bank withdrawal is not supported."); 183 System.exit(0); 184 } 185 c++; 186 } 187 188 189 190 int d = 0; 191 while (d<need.size()){ 192 String S = ""; 193 if (need.get(d) < 0) { 194 S = "存款"; 195 } else { 196 S = "取款"; 197 } 198 for (int j = 0; j < chinaUnionPay.CCB.getAccountList().size(); j++) { 199 for (int jj = 0; jj < chinaUnionPay.CCB.getAccountList().get(j).getCardNumber().size(); jj++) { 200 if (chinaUnionPay.CCB.getAccountList().get(j).getCardNumber().get(jj).equals(CardNumber.get(d))) { 201 if(Password.get(d).equals("")){ 202 String format2 = String.format("%.2f", chinaUnionPay.CCB.getAccountList().get(j).getBlance()); 203 System.out.println("¥" + format2); 204 } 205 else { 206 chinaUnionPay.CCB.getAccountList().get(j).changeBalance(need.get(d)); 207 String format1 = String.format("%.2f", Math.abs(need.get(d))); 208 String format2 = String.format("%.2f", chinaUnionPay.CCB.getAccountList().get(j).getBlance()); 209 System.out.println(chinaUnionPay.CCB.getAccountList().get(j).getNAME() + "在中国建设银行的" + ATMNumber.get(d) + "号ATM机上" + S + "¥" + format1); 210 System.out.println("当前余额为¥" + format2); 211 } 212 213 } 214 215 } 216 } 217 for (int ii = 0; ii < chinaUnionPay.ICBC.getAccountList().size(); ii++) { 218 for (int iii = 0; iii < chinaUnionPay.ICBC.getAccountList().get(ii).getCardNumber().size(); iii++) { 219 if (chinaUnionPay.ICBC.getAccountList().get(ii).getCardNumber().get(iii).equals(CardNumber.get(d))) { 220 if(Password.get(d).equals("")){ 221 String format2 = String.format("%.2f", chinaUnionPay.ICBC.getAccountList().get(ii).getBlance()); 222 System.out.println("¥" + format2); 223 } 224 else { 225 chinaUnionPay.ICBC.getAccountList().get(ii).changeBalance(need.get(d)); 226 String format1 = String.format("%.2f", Math.abs(need.get(d))); 227 String format2 = String.format("%.2f", chinaUnionPay.ICBC.getAccountList().get(ii).getBlance()); 228 System.out.println(chinaUnionPay.ICBC.getAccountList().get(ii).getNAME() + "在中国工商银行的" + ATMNumber.get(d) + "号ATM机上" + S + "¥" + format1); 229 System.out.println("当前余额为¥" + format2); 230 } 231 } 232 233 } 234 } 235 236 d++; 237 } 238 239 240 } 241 }
接下来的迭代在完成信用卡功能的实现下并针对上次的问题进行了优化

首先是数据校验和异常处理分别设计两个类
1 class ValidateData { 2 /** 3 * 校验卡号是否存在 4 * @param unionPay 5 * @param cardNO 6 * @return 7 */ 8 public static Card getCardbyCardNO(UnionPay unionPay,String cardNO) { 9 Card card = null; 10 Iterator<Bank> bankItr = unionPay.getBankList().iterator(); 11 12 while(bankItr.hasNext()) { 13 ArrayList<Account> accountList = bankItr.next().getAccountList(); 14 Iterator<Account> accountItr = accountList.iterator(); 15 while(accountItr.hasNext()) { 16 ArrayList<Card> cardList = accountItr.next().getList(); 17 Iterator<Card> cardItr = cardList.iterator(); 18 while(cardItr.hasNext()) { 19 card = cardItr.next(); 20 if(card.getCardNO().equals(cardNO)) { 21 return card; 22 } 23 } 24 } 25 } 26 return null; 27 } 28 29 /** 30 * 校验ATM ID是否存在 31 * @param unionPay 32 * @param ATMID 33 * @return 34 */ 35 public static ATM getATMbyATMID(UnionPay unionPay,String ATMID) { 36 Iterator<Bank> bankItr = unionPay.getBankList().iterator(); 37 Bank bank = null; 38 ATM aTM = null; 39 40 while(bankItr.hasNext()) { 41 bank = bankItr.next(); 42 Iterator<ATM> aTMItr = bank.getATMList().iterator(); 43 44 while(aTMItr.hasNext()) { 45 aTM = aTMItr.next(); 46 if(aTM.getATMID().equals(ATMID)) { 47 return aTM; 48 } 49 } 50 } 51 return null; 52 } 53 public void validateCardnumber(UnionPay unionPay, String cardNO) { 54 /** 55 * 校验该卡是否存在 56 */ 57 Card card = ValidateData.getCardbyCardNO(unionPay, cardNO); 58 if (card == null) { 59 System.out.println("Sorry,this card does not exist."); 60 System.exit(0); 61 } 62 63 } 64 65 public void validatePassword (UnionPay unionPay,String cardNO,String cardPassword){ 66 Account account = Account.getAmountbyCardNO(cardNO); 67 double balance = account.getBalance(); 68 Card card = ValidateData.getCardbyCardNO(unionPay, cardNO); 69 /** 70 * 校验卡密码是否正确 71 */ 72 if (!card.getCardPassword().equals(cardPassword)) { 73 System.out.println("Sorry,your password is wrong."); 74 System.exit(0); 75 } 76 } 77 78 public void validateBalance(UnionPay unionPay,String cardNO,double amount,String ATMID){ 79 Account account = Account.getAmountbyCardNO(cardNO); 80 double balance = account.getBalance(); 81 /** 82 * 校验取款金额是否大于余额 83 */ 84 if(account.getType().equals("normal")){ 85 if(!validateBank(unionPay,cardNO,ATMID)) { 86 ATM aTM = ValidateData.getATMbyATMID(unionPay, ATMID); 87 amount = amount * 1+aTM.getBank().getCrossbankcharge(); 88 } 89 if (amount > balance) { 90 System.out.println("Sorry,your account balance is insufficient."); 91 System.exit(0); 92 } 93 } 94 else { 95 amount = amount * 1.05; 96 if (amount > balance + 50000) { 97 System.out.println("Sorry,your account balance is insufficient."); 98 System.exit(0); 99 } 100 } 101 } 102 103 public boolean validateBank(UnionPay unionPay, String cardNO, String ATMID){ 104 Account account = Account.getAmountbyCardNO(cardNO); 105 double balance = account.getBalance(); 106 ATM aTM = ValidateData.getATMbyATMID(unionPay, ATMID); 107 /** 108 * 校验是否为跨行取款 109 */ 110 if (account.getBank().getBankNO() != aTM.getBank().getBankNO()) { 111 return false; 112 } 113 return true; 114 } 115 116 public void validateATNID (UnionPay unionPay,String ATMID){ 117 /** 118 * 校验ATM是否存在 119 */ 120 ATM aTM = ValidateData.getATMbyATMID(unionPay, ATMID); 121 if(aTM == null) { 122 System.out.println("Sorry,the ATM's id is wrong."); 123 System.exit(0); 124 } 125 } 126 public void validateALL(UnionPay unionPay, String cardNO, String cardPassword, String ATMID, double amount){ 127 validateCardnumber(unionPay,cardNO); 128 validateATNID(unionPay,ATMID); 129 validateBalance(unionPay,cardNO,amount,ATMID); 130 validatePassword(unionPay,cardNO,cardPassword); 131 } 132 }
这里是异常处理类
1 class Withdraw { 2 private UnionPay unionPay; 3 private String cardNO; 4 private String cardPassword; 5 private String ATMID; 6 private double amount; 7 private Bank goalBank; 8 ValidateData vd = new ValidateData(); 9 public Withdraw() { 10 super(); 11 // TODO Auto-generated constructor stub 12 } 13 14 15 public Withdraw(UnionPay unionPay, String cardNO, String cardPassword, String aTMID, double amount) { 16 super(); 17 this.unionPay = unionPay; 18 this.cardNO = cardNO; 19 this.cardPassword = cardPassword; 20 ATMID = aTMID; 21 this.amount = amount; 22 } 23 24 public Withdraw(String cardNO, String cardPassword, String aTMID, double amount) { 25 super(); 26 this.cardNO = cardNO; 27 this.cardPassword = cardPassword; 28 ATMID = aTMID; 29 this.amount = amount; 30 } 31 32 public void showthend(){ 33 Account account = Account.getAmountbyCardNO(cardNO); 34 if(amount >= 0) { 35 showResult(account,1); 36 }else { 37 showResult(account,0); 38 } 39 } 40 41 public void playblance() { 42 Account account = Account.getAmountbyCardNO(cardNO); 43 ATM aTM = ValidateData.getATMbyATMID(unionPay, ATMID); 44 goalBank = aTM.getBank(); 45 double overdraw = 0; 46 double cross = 0; 47 double balance = account.getBalance(); 48 if (balance>0) { 49 if (amount > balance) { 50 overdraw = (amount - balance) * 0.05; 51 } 52 } 53 else{ 54 overdraw = amount * 0.05; 55 } 56 //跨行利率 57 if (goalBank.getBankNO() != account.getBank().getBankNO()) { 58 cross = amount * goalBank.getCrossbankcharge(); 59 60 } 61 account.setBalance(balance-overdraw-amount-cross); 62 //取款更新余额操作 63 } 64 public void showResult(Account account,int flag) { 65 String type = ""; 66 if(flag == 1) { 67 type = "取款"; 68 }else { 69 type = "存款"; 70 amount *= -1; 71 } 72 String userName = account.getUser().getName(); 73 String bankName = goalBank.getBankName(); 74 System.out.println("业务:"+type+" "+userName + "在" + 75 bankName + "的" + ATMID + "号ATM机上" + type + String.format("¥%.2f", amount)); 76 System.out.println("当前余额为" + String.format("¥%.2f", account.getBalance())); 77 } 78 }
后续数据处理,即对卡内余额进行更改
对account继续继承,继承子类为信用账户
1 class CreditAccount extends Account { 2 private double Creditbalance = 0; 3 4 public CreditAccount(String accountNO, double balance, User user, Bank bank, String type, double creditbalance) { 5 super(accountNO, balance, user, bank, type); 6 Creditbalance = creditbalance; 7 } 8 9 public double getCreditbalance() { 10 return Creditbalance; 11 } 12 13 public void setCreditbalance(double creditbalance) { 14 Creditbalance = creditbalance; 15 } 16 }
最后一点不同为设计了通过卡号找账户的方法
1 public static Account getAmountbyCardNO(String cardNO) { 2 Iterator<Card> cardItr = Account.list.iterator(); 3 Card card = null; 4 5 while(cardItr.hasNext()) { 6 card = cardItr.next(); 7 if(card.getCardNO().equals(cardNO)) { 8 return card.getAccount(); 9 } 10 } 11 12 return null; 13 }
三、采坑心得
由于第一次ATM中的一些问题在迭代中得到优化,所以在此就不就行赘述。
注重说第二次迭代中的问题
Account与CreaditAccount,这二者关系没有考虑好,不应该继承二者虽说类似,但当还有其他的账户种类时就不好继承。
四、改进建议:
刚才说的Account与CreaditAccount,这里可以写一个抽象账户类来让二者继承
1 abstract class AbstractAccount { 2 private String accountNO;// 账号 3 private double balance = 0;// 余额 4 private User user = null;// 用户 5 private Bank bank = null;// 所属银行 6 private double ratio = 0;// 透支取款手续费 7 private double overdrawAmount = 0;// 可透支金额 8 private static ArrayList<Card> list = new ArrayList<Card>(); 9 10 public AbstractAccount() { 11 super(); 12 // TODO Auto-generated constructor stub 13 } 14 15 public AbstractAccount(String accountNO, double balance, User user, Bank bank, double ratio, 16 double overdrawAmount) { 17 super(); 18 this.accountNO = accountNO; 19 this.balance = balance; 20 this.user = user; 21 this.bank = bank; 22 this.ratio = ratio; 23 this.overdrawAmount = overdrawAmount; 24 } 25 26 public String getAccountNO() { 27 return accountNO; 28 } 29 30 public void setAccountNO(String accountNO) { 31 this.accountNO = accountNO; 32 } 33 34 public double getBalance() { 35 return balance; 36 } 37 38 public void setBalance(double balance) { 39 this.balance = balance; 40 } 41 42 public User getUser() { 43 return user; 44 } 45 46 public void setUser(User user) { 47 this.user = user; 48 } 49 50 public double getOverdrawAmount() { 51 return overdrawAmount; 52 } 53 54 public void setOverdrawAmount(double overdrawAmount) { 55 this.overdrawAmount = overdrawAmount; 56 } 57 58 public Bank getBank() { 59 return bank; 60 } 61 62 public void setBank(Bank bank) { 63 this.bank = bank; 64 } 65 66 public double getRatio() { 67 return ratio; 68 } 69 70 public void setRatio(double ratio) { 71 this.ratio = ratio; 72 } 73 74 public static ArrayList<Card> getList() { 75 return list; 76 } 77 78 public static void setList(ArrayList<Card> list) { 79 AbstractAccount.list = list; 80 } 81 82 public void addCard(Card card) { 83 list.add(card); 84 } 85 86 public void removeCard(Card card) { 87 list.remove(card); 88 } 89 }
这样再有新的账户,就可以直接继承这个账户类
五、总结:
1.了解多态优缺点
多态的好处:提高了程序的扩展性 具体:定义方法的时候,使用父类型作为参数,将来在使用的时候,使用具体的子类型参与操作
弊端:不能使用子类的特有功能 如果想要调用子类里面的方法,就需要向下转型。
2.在类似这次ATM这种现实工程中,不要只为了满足现在的需求,要多考虑未来的需求,考虑代码的可扩展性。

浙公网安备 33010602011771号