那些年,我写过的面向对象 (Java)

来源:互联网 发布:外国人评论中国 知乎 编辑:程序博客网 时间:2024/06/10 06:34

程序1.

//时间:2013.11.07//功能:创建账户Account 类 并且把它的信息输出出来public class AccountShow {public static void main (String[] args) {Account account = new Account(1122, 20000);  //创建一个对象,ID为1122,余额20000Account.setAnnualInterestRate(4.5);//设置年利率 4.5%account.withdraw(2500);//取款 2500account.deposit(3000);//存款3000System.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;}}


 

 

原创粉丝点击