注册 登录
编程论坛 JAVA论坛

学习面向对象中依赖关系,修改程序出现问题,不会建set,get感觉(新手啊,然后程序都敲好了)

s人圭日月 发布于 2016-10-24 22:12, 3099 次点击
程序代码:
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感觉。希望大神帮我看看,谢谢
6 回复
#2
s人圭日月2016-10-24 22:15
回复 楼主 s人圭日月
就是原本的日志输出我是知道的,
String[] logInfo =account1.getlogInfo();
        for(String log:logInfo){
            System.out.println(log);

但是那个依赖关系中的引用是缺了一个BankID中set,get不知道怎么去加
#3
s人圭日月2016-10-24 22:18
然后原文我注释掉的地方有个小错误,就是index只打了inde  不要在意啊 ,自己源程序自己当时不下心少打就没改
#4
纳兰不羁2016-11-19 20:18
利用快捷键或者功能区直接可以生成get,set方法
#5
qwe8851677592016-11-26 23:22
4楼说的对,不怕麻烦的话可以自己敲
public type get***()
{
......
}

public void set***()
{
.....
}

#6
Vsnow2016-11-27 19:40
在属性下面的空白处单击鼠标右键,选择source,选择Generate Getters and Setters
#7
andymqq2016-12-04 11:46
回复 楼主 s人圭日月
你用的是Eclipse吗,用快捷键alt+shift+S,选择Generate Getter and Setters,选中你要添加get、set方法的属性就OK了
1