天天看點

static應用舉例

1 package com.fu.statictest;
 2 
 3 /**
 4  * static應用舉例:
 5  * 編寫一個類實作銀行賬戶的概念,包含的屬性有 “賬号”、“密碼”、“存款餘額”、“利率”、“最小餘額”,
 6  * 定義封裝這些屬性的方法,賬戶自動生成。
 7  * 編寫主類,使用銀行賬戶類,輸入,輸出3個儲蓄的上述資訊。
 8  * 考慮:哪些屬性可以設計為static屬性
 9  *
10  *
11  *
12  */
13 public class Account {
14     private int id;
15     private String password = "000000";
16     private double balance = 0.0;
17     private static double interestRate;
18     private static double minBalance = 1.0;
19     private static int init = 1001;//用于自動生成賬戶使用的
20 
21     public Account(){
22         id = init++;
23     }
24 
25     public Account(String password, double balance) {
26         this();
27         this.password = password;
28         this.balance = balance;
29     }
30 
31     public int getId(){
32         return id;
33     }
34 
35     public String getPassword() {
36         return password;
37     }
38 
39     public void setPassword(String password) {
40         this.password = password;
41     }
42 
43     public double getBalance() {
44         return balance;
45     }
46 
47     public static double getInterestRate() {
48         return interestRate;
49     }
50 
51     public static void setInterestRate(double interestRate) {
52         Account.interestRate = interestRate;
53     }
54 
55     public static double getMinBalance() {
56         return minBalance;
57     }
58 
59     public static void setMinBalance(double minBalance) {
60         Account.minBalance = minBalance;
61     }
62 
63     @Override
64     public String toString() {
65         return "Account{" +
66                 "id=" + id +
67                 ", password='" + password + '\'' +
68                 ", balance=" + balance +
69                 '}';
70     }
71 }      
package com.fu.statictest;

public class AccountTest {
    public static void main(String[] args) {
        Account acct1 = new Account("666666",20);
        Account acct2 = new Account("123456",40);
        Account acct3 = new Account("987654",100);
        Account.setInterestRate(0.02);
        System.out.println("賬戶:"+ acct1.getId() +" 密碼:"+ acct1.getPassword()+ " 餘額:"+ acct1.getBalance());
        System.out.println(acct1);
        System.out.println(acct2);
        System.out.println(acct3);
        System.out.println("利率:"+Account.getInterestRate());
        System.out.println("最小餘額:"+Account.getMinBalance());


    }
}      
static應用舉例

此為本人學習筆記,若有錯誤,請不吝賜教