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());
}
}