DALLASCAO.COM

Site of Cao Shouguang, English to Chinese translator

public class and class


public class SavingsAccount {
	
	public static void main(String[] args) {
		AccountCaculation myAccount = new AccountCaculation(1000).deposit(2000);
		AccountCaculation yourAccount = new AccountCaculation(2000).deposit(5000);		
		myAccount.printBalance();
		yourAccount.printBalance();
		System.out.println(AccountCaculation.totalBalance);
	}

}
class AccountCaculation {
	/**
	 * output:
	 * 
	 * 3000
	 * 7000
	 * 10000
	 */
	int balance;	
	static int totalBalance;
	AccountCaculation(int balance) {
		this.balance = balance;
	}
	AccountCaculation deposit(int amount) {
		balance += amount;
		totalBalance = totalBalance + balance;
		return this;
	}
	
	void printBalance() {
		System.out.println(balance);		
	}
	
}

Leave a Comment