Java 封装+构造器+this 小测试

 1 package com.bytezero.account;
 2 
 3 
 4 public class Account 
 5 {
 6     private int id;     //账号
 7     private double balance;  //余额
 8     private double annualInterestRate; //年利率
 9     
10     
11     //构造器
12     public Account(int id, double balance, double annualInterestRate) 
13     {
14         
15         this.id = id;
16         this.balance = balance;
17         this.annualInterestRate = annualInterestRate;
18     }
19 
20 
21     // set  get
22     public int getId() 
23     {
24         return id;
25     }
26 
27 
28     public void setId(int id)
29     {
30         this.id = id;
31     }
32 
33 
34     public double getBalance() 
35     {
36         return balance;
37     }
38 
39 
40     public void setBalance(double balance)
41     {
42         this.balance = balance;
43     }
44 
45 
46     public double getAnnualInterestRate() 
47     {
48         return annualInterestRate;
49     }
50 
51 
52     public void setAnnualInterestRate(double annualInterestRate) 
53     {
54         this.annualInterestRate = annualInterestRate;
55     }
56     
57     //方法
58     public void withdraw(double amount)   //取钱
59     {
60         if(balance < amount)
61         {
62             System.out.println("取款失败!!!!");
63             return;
64         }
65         balance -= amount;
66         System.out.println("成功取出:"+ amount);
67     }
68     public void dsposit(double amount)  //存钱
69     {
70         if(amount > 0)
71         {
72             balance += amount;
73             System.out.println("成功存入"+amount);
74         }
75     }
76     
77     
78     
79     
80     
81     
82 }
 1 package com.bytezero.account;
 2 
 3 public class Customer 
 4 {
 5     private String firstName;
 6     private String laseName;
 7     private Account account;
 8     
 9     
10     
11     public Customer(String f,String l)
12     {
13         this.firstName = f;
14         this.laseName = l;
15     }
16 
17 
18 
19     public Account getAccount() {
20         return account;
21     }
22 
23 
24 
25     public void setAccount(Account account) {
26         this.account = account;
27     }
28 
29 
30 
31     public String getFirstName() {
32         return firstName;
33     }
34 
35 
36 
37     public String getLaseName() {
38         return laseName;
39     }
40     
41     
42     
43     
44     
45     
46     
47     
48 }
 1 package com.bytezero.account;
 2 
 3 /**
 4  * 
 5  * @Description 
 6  * @author  Bytezero·zhenglei!      Email:420498246@qq.com
 7  * @version
 8  * @date 2021年9月15日下午9:55:22
 9  * @  小测试
10  *
11  */
12 public class CustomerTest
13 {
14     public static void main(String[] args) 
15     {
16         Customer cust = new Customer("Jane","Smith");
17         
18         Account acct = new Account(1000, 2000, 0.0123);
19         
20         cust.setAccount(acct);
21         
22         cust.getAccount().dsposit(100);
23         cust.getAccount().withdraw(960);
24         cust.getAccount().withdraw(2000);
25         
26         System.out.println("客户叫:"+cust.getFirstName()+cust.getLaseName());
27         System.out.println("客户的id:"+cust.getAccount().getId());
28         
29     }
30 }

 

posted on 2021-09-15 22:15  Bytezero!  阅读(48)  评论(0)    收藏  举报