java面向对象 怎么写_那些年,我写过的面向对象 (Java)

程序1.

//时间:2013.11.07

//功能:创建账户Account 类 并且把它的信息输出出来

public class AccountShow {

public static void main (String[] args) {

Account account = new Account(1122, 20000); //创建一个对象,ID为1122,余额20000

Account.setAnnualInterestRate(4.5);//设置年利率 4.5%

account.withdraw(2500);//取款 2500

account.deposit(3000);//存款3000

System.out.println("Balance is " + account.getBalance()); //打印余额

System.out.println("Monthly interest is " +//打印月利率

account.getMonthlyInterest());

System.out.println("This account was created at " +//打印开户日期

account.getDateCreated());

}

}

class Account {//创建Account类

private int id;//创建Account的成员及方法

private double balance;

private static double annualInterestRate;

private java.util.Date dateCreated;

public Account() { //创建无参构造方法

dateCreated = new java.util.Date();

}

public Account(int newId, double newBalance) { //创建有id,余额 的构造方法

id = newId;

balance = newBalance;

dateCreated = new java.util.Date();

}

public int getId() {//创建getId()方法

return this.id;

}

public double getBalance() {//创建getBalance()方法

return balance;

}

public static double getAnnualInterestRate() {//创建getAnnualInterestRate()方法

return annualInterestRate;

}

public void setId(int newId) { //创建setId(int newId)方法

id = newId;

}

public void setBalance(double newBalance) {//创建setBalance()方法

balance = newBalance;

}

//创建setAnnualInterestRate(double newAnnualInterestRate)方法

public static void setAnnualInterestRate(double newAnnualInterestRate) {

annualInterestRate = newAnnualInterestRate;

}

public double getMonthlyInterest() {//创建getMonthlyInterest()方法

return balance * (annualInterestRate / 1200);

}

public java.util.Date getDateCreated() {//创建getDateCreated() 方法

return dateCreated;

}

public void withdraw(double amount) {//创建withdraw(double amount)方法

balance -= amount;

}

public void deposit(double amount) {//创建deposit(double amount)方法

balance += amount;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值