admin管理员组

文章数量:1648371

类与对象,作为一个面向对象的程序员,对象和类是经常会碰到的。只有什么是对象,各位,敢说“万物皆对象”吗?类是同一类型的对象的合约,模板或蓝本。(其实,我本人不喜欢这些咬文嚼字的东东),不多说,直接代码解释。

package com.exercise;

import java.util.Date;

/**
 * 账户类 Account
 * Created by mark on 2016/8/5.
 */
public class Account {

    private int id = 0;
    private double balance = 0.0;
    private double annualInterestRate = 0.0;// 当前利率
    private Date dateCreated;// 账户的开户日期

    public Account() {
        dateCreated = new Date();
    }

    public Account(int id, double balance) {
        this.id = id;
        this.balance = balance;
        dateCreated = new Date();
    }

    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 Date getDateCreated() {
        return dateCreated;
    }


    public double getMonthlyInterestRate() {
        return balance * (annualInterestRate / 1200);
    }

    public void withDraw(double account) {
        balance -= account;
    }

    public void deposit(double account) {
        balance += account;
    }


    public static void main(String[] args) {

        Account account = new Account(1122, 20000);
        account.setAnnualInterestRate(4.5);
        account.withDraw(2500);
        account.deposit(3000);

        System.out.println("余额为: " + account.getBalance());
        System.out.println("月利息为: " + account.getMonthlyInterestRate());
        System.out.println("账户的开户日期为: " + account.getDateCreated());
    }

}

本文标签: 账户Account