随笔 - 322  文章 - 0  评论 - 4  阅读 - 77146

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   Bytezero!  阅读(34)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示