Java 小案例+super使用

 1 package com.bytezero.supertest3;
 2 
 3 /**
 4  * 
 5  * @Description    AccountTest类
 6  * @author  Bytezero·zhenglei!      Email:420498246@qq.com
 7  * @version
 8  * @date 2021年9月20日上午9:52:27
 9  * @
10  *
11  */
12 public class AccountTest
13 {
14     public static void main(String[] args) {
15         
16         Account acct = new Account(1122,20000,0.045);
17         
18         acct.withdraw(30000);
19         System.out.println("您的账户余额为:"+acct.getBalance());
20         
21         acct.withdraw(2500);
22         System.out.println("您的账户余额为:"+acct.getBalance());
23 
24         acct.deposit(3000);
25         System.out.println("您的账户余额为:"+acct.getBalance());
26         
27         System.out.println("月利率为:"+(acct.getMonthlyInterest() *100) +"%");
28         
29         
30         
31     }
32 }
 1 package com.bytezero.supertest3;
 2 
 3 public class Account
 4 {
 5     private int id;           //账号
 6     private double balance;   //余额
 7     private double annualInterestRate;   //年利率
 8     
 9     //构造器
10     public Account(int id, double balance, double annualInterestRate) 
11     {
12         super();
13         this.id = id;
14         this.balance = balance;
15         this.annualInterestRate = annualInterestRate;
16     }
17     
18     //get  set
19     
20     public int getId()
21     {
22         return id;
23     }
24     public void setId(int id)
25     {
26         this.id = id;
27     }
28 
29     public double getBalance() 
30     {
31         return balance;
32     }
33 
34     public void setBalance(double balance) 
35     {
36         this.balance = balance;
37     }
38 
39     public double getAnnualInterestRate() 
40     {
41         return annualInterestRate;
42     }
43 
44     public void setAnnualInterestRate(double annualInterestRate) 
45     {
46         this.annualInterestRate = annualInterestRate;
47     }
48     
49     //返回月利率
50     public double getMonthlyInterest()
51     {
52         return annualInterestRate / 12;
53     }
54     
55     //取钱
56     public void withdraw(double amount)
57     {
58         if(balance >= amount)
59         {
60             balance -= amount;
61             return;
62         }
63         System.out.println("余额不足!");
64     }
65     
66     
67     //存钱
68     public void deposit(double amount)
69     {
70         if(amount > 0)
71         {
72             balance += amount;
73         }
74         
75         
76         
77         
78         
79     }
80     
81     
82     
83     
84 }

 

 

 

 1 package com.bytezero.supertest3;
 2 
 3 /**
 4  * 
 5  * @Description 
 6  * @author  Bytezero·zhenglei!      Email:420498246@qq.com
 7  * @version
 8  * @date 2021年9月20日上午11:08:11
 9  * @
10  *
11  */
12 public class CheckAccountTest
13 {
14     public static void main(String[] args)
15     {
16         CheckAccount acct = new CheckAccount(1122,20000,0.045,5000);
17         
18         acct.withdraw(5000);
19         System.out.println("您的账户余额为:"+ acct.getBalance());
20         System.out.println("您的可透支额度为:"+acct.getOverdraft());
21         
22         acct.withdraw(18000);
23         System.out.println("您的账户余额为:"+ acct.getBalance());
24         System.out.println("您的可透支额度为:"+acct.getOverdraft());
25         
26         acct.withdraw(3000);
27         System.out.println("您的账户余额为:"+ acct.getBalance());
28         System.out.println("您的可透支额度为:"+acct.getOverdraft());
29         
30         
31         
32         
33     }
34 }
 1 package com.bytezero.supertest3;
 2 
 3 /*
 4  * 
 5  * 
 6  */
 7 public class CheckAccount extends Account
 8 {
 9     private double overdraft; //可透支的限额
10     
11     
12     
13     public CheckAccount(int id, double balance, double annualInterestRate,double overdraft)
14     {
15         super(id, balance, annualInterestRate);
16         this.overdraft = overdraft;
17     }
18     
19     
20     
21     
22     public double getOverdraft() {
23         return overdraft;
24     }
25 
26 
27 
28 
29     public void setOverdraft(double overdraft) {
30         this.overdraft = overdraft;
31     }
32 
33 
34 
35 
36     @Override
37     public void withdraw(double amount) 
38     {
39         if(getBalance() >= amount)  //余额足够
40         {
41             //方式一 
42             //setBalance(getBalance() - amount);
43             
44             //方式二
45             super.withdraw(amount);
46             //方式三
47             //可以将父类的 private double balance;   //余额 改为 
48                          //protected double balance
49             
50             
51             
52         }
53         //余额不够   透支额度 + 余额消费
54         else if(overdraft >=( amount - getBalance())) 
55         {
56             overdraft -= (amount - getBalance());
57             
58             //setBalance(0);
59             //或者
60             super.withdraw(getBalance());
61             
62         }
63         else
64         {
65             System.out.println("超过可透支限额!");
66         }
67         
68     }
69     
70     
71     
72     
73     
74 
75 }

 

posted on 2021-09-20 11:09  Bytezero!  阅读(45)  评论(0编辑  收藏  举报