This关键字练习

Posted afangfang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

 

以上是关于This关键字练习的主要内容,如果未能解决你的问题,请参考以下文章

java this 关键字练习

很实用的JQuery代码片段(转)

laravel特殊功能代码片段集合

vue2.0 代码功能片段

spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段

在 webview_flutter 中启用捏合和缩放,在哪里添加代码片段 [this.webView.getSettings().setBuiltInZoomControls(true);]