标签:ast private ble str osi name package div code
Account:
package com.aff.ex; public class Account { private int id;// 账号 private double balance;// 余额 private double annualInterestRate;// 年利率 public Account(int id, double balance, double annualInterestRate) { this.id = id; this.balance = balance; this.annualInterestRate = annualInterestRate; } public int getId() { return id; } public void setId(int id) { this.id = id; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public double getAnnualInterestRate() { return annualInterestRate; } public void setAnnualInterestRate(double annualInterestRate) { this.annualInterestRate = annualInterestRate; } // 取钱 public void withdraw(double amount) { if (balance >= amount) { balance -= amount; System.out.println("成功取出" + amount); } else { System.out.println("余额不足"); } } // 存钱 public void deposit(double amount) { balance += amount; System.out.println("成功存入" + amount); } }
Customer:
package com.aff.ex; public class Customer { private String fristName; private String lastName; private Account account; public Customer(String f, String l) { this.fristName = f; this.lastName = l; } public Account getAccount() { return account; } public void setAccount(Account account) { this.account = account; } public String getFristName() { return fristName; } public String getLastName() { return lastName; } }
TestCustomer:
package com.aff.ex; //创建一个Customer,起名叫Jane Smith, //他账号为1000 ,余额2000,年利率1.23% //操作: 存入100,再取出950,再取出2000,打印Jane Smith的基本信息 public class TestCustomer { public static void main(String[] args) { Customer cus = new Customer("Jane", "Smith"); cus.setAccount(new Account(1000, 2000, 0.0123));// 使用匿名的方式创建 Account acc = cus.getAccount(); acc.deposit(100); acc.withdraw(960); acc.withdraw(2000); System.out.println("Customer[" + cus.getLastName() + "," + cus.getFristName()
+ "] has a account: id is" + acc.getId() + "annualInterestRate" + acc.getAnnualInterestRate() * 100 + "%" + "balance is" + acc.getBalance()); } }
输出结果:
成功存入100.0
成功取出960.0
余额不足
Customer[Smith,Jane] has a account: id is1000annualInterestRate1.23%balance is1140.0
标签:ast private ble str osi name package div code
原文地址:https://www.cnblogs.com/afangfang/p/12515121.html