• This关键字练习


    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

     
    All that work will definitely pay off
  • 相关阅读:
    C++类继承内存布局(二)
    C++类继承内存布局(一)
    排序算法
    python 文件路径问题
    python map对象
    python reduce() 函数
    python eval 用法
    PyCharm引入自定义类报错
    Python中的lambda的简单介绍
    python time模块和datetime模块
  • 原文地址:https://www.cnblogs.com/afangfang/p/12515121.html
Copyright © 2020-2023  润新知