学习面向对象中依赖关系,修改程序出现问题,不会建set,get感觉(新手啊,然后程序都敲好了)
程序代码:
package public class BankId { //声明属性 private String id; private String pwd; private double balance; //声明数组,保存日志 private String[] logInfo = new String[3]; private int index; //有三个参数的构造方法 BankId(String id,String pwd,double balance){ //System.out.println("BankId(String id,String pwd,double balance)"); this.id = id; this.pwd=pwd; this.balance = balance; } //查看余额 double getBalance(){ return balance; } //存款活期 public void deposit(double amount){ balance += amount; Logger.log(this,"日志,存款成功"+amount); //if(inde>2){ // inde = 0; //} //logInfo[inde]="活期的存款为:"+amount; //inde++; } //存款 定期 public void deposit(double amount, int term){ balance += amount; Logger.log(this,"日志,存款成功"+amount); //if(inde>2){ // inde = 0; //} //logInfo[inde]="活期的存款为:"+amount; //inde++; } //取款,不能透支 public boolean withdraw(double amount){ if (amount > balance){ Logger.log(this,"日志,存款失败"+amount); //if(inde>2){ // inde = 0; //} //logInfo[inde]="取款失败:"+amount; //inde++; return false; }else{ balance -= amount; Logger.log(this,"日志,存款成功"+amount); //if(inde>2){ // inde = 0; //} //logInfo[inde]="取款成功:"+amount; //inde++; return true; } } //返回日志数组 public String[] getlogInfo(){ return this.logInfo; } }
程序代码:
package class Logger { public static void log(BankId account,String msg){ String[] logInfo =account.getlogInfo(); int index = account.getindex(); if(index>2){ index=0; } logInfo[index]=msg; index++; account.setindex(index);//就setloginfo和setloginfo没有前面定义 } public static void printLog(BankId account){ String[] LogInfo = account.getlogInfo(); for (String s:logInfo){ if(s!=null){ System.out.println("日志"+s); } else{ break; } } } }
程序代码:
package public class TestBankId { /** * @param args */ public static void main(String[] args) { BankId account1 = new BankId("312214","231244",5200); account1.deposit(2222); account1.withdraw(1400); account1.withdraw(1400); String[] logInfo =account1.getlogInfo(); Logger.printLog(account1); } }就是这个程序时让我修改“日志保存”运用依赖关系,建一个类中引用。但是我有点不会,缺了一个set,get感觉。希望大神帮我看看,谢谢