首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

求大神见见下列代码的this(红色的字体)是什么意思?还可以用什么方式代替this

2013-10-29 
求大神看看下列代码的this(红色的字体)是什么意思?还可以用什么方式代替thisclass InsufficientFundsExcep

求大神看看下列代码的this(红色的字体)是什么意思?还可以用什么方式代替this
class InsufficientFundsException extends Exception {  
    private Bank excepbank; // 银行对象  
    private double excepAmount; // 要取的钱  
  
    InsufficientFundsException(Bank ba, double dAmount) {  
        excepbank = ba;  
        excepAmount = dAmount;  
    }  
  
    public String excepMessage() {  
        String str = "The balance is" + excepbank.balance + "\n"  
                + "The withdrawal was" + excepAmount;  
        return str;  
    }  
}
class Bank {  
    double balance;// 存款数  
  
    Bank(double balance) {  
        this.balance = balance;  
    }  
  
    public void deposite(double dAmount) {  
        if (dAmount > 0.0)  
            balance += dAmount;  
    }  
  
    public void withdrawal(double dAmount) throws InsufficientFundsException {  
        if (balance < dAmount)  
            throw new InsufficientFundsException(this, dAmount);  
            balance = balance - dAmount;  
    }  
    public void showBalance() {  
        System.out.println("The balance is " + (int) balance);  
    }  
}  
public class ExceptionDemo {

public static void main(String[] args) {
 try {  
            Bank ba = new Bank(50);  
            ba.withdrawal(100);  ba.showBalance();
            System.out.println("Withdrawal successful!");  
        } catch (InsufficientFundsException e) {  
        e.printStackTrace();
            System.out.println(e.excepMessage());  
        }  

}

}
[解决办法]
this代码Bank这个类的对象。
或者你可以吧这个对象在外面定义,然后传进来代替this
[解决办法]
this在Bank类里面用的,代表bank对象。
[解决办法]
代表当前你的类。
[解决办法]
当前操作对象 

热点排行