UML示意图:最上方Account为类
中部(声明属性):-(表示访问权限域为private)id(属性名):double(数据类型)
下部(定义方法):+(表示访问权限域为public)Account(方法名)(int,double,double)(接收的参数)
1.创建account类:
{
private int id;
private double balance;
private double annualInterestRate;
private Date dateCreated;
public Account(int id,double balance,double annualInterestRate){
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
dateCreated = new Date();
}
public boolean withDraw(double amount){
if(balance<amount)
return false;
balance-=amount;
return true;
}
public void deposite(double amount){
balance+=amount;
}
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 void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}
}
2.生成一个对象:
{
public static void main(String[] args){
Account testAccount = new Account(11112,20000,4);
if(testAccount.withDraw(200000))
System.out.println("withdraw successful");
else
System.out.println("withdraw not successful");
testAccount.deposite(30000);
System.out.println("id:"+testAccount.getId()+" "+
"balance:"+testAccount.getBalance()+" "+
"annualInterestRate:"+testAccount.getAnnualInterestRate());
}
}
运行结果:
withdraw not successful
id:11112 balance:50000.0 annualInterestRate:4.0
标签:11,annualInterestRate,java,22,double,id,return,balance,public From: https://blog.csdn.net/2401_82415172/article/details/143981053