构造函数实现动态账户余额

168 阅读1分钟
package zhanghu;
/**
 * 创建一个账户Account类,
 * 该类有id:账户号码(长整数),
 * password:账户密码,name:真实姓名,personId:身份证号码
 * 字符串类型,email:客户的电子邮箱,
 * balance:账户余额.
 * 方法:deposit: 存款方法,参数是double型的金额;
 * withdraw:取款方法,参数是double型的金额.
 * 构造方法:有参和无参,有参构造方法用于设置必要的属性。
 */
public class Test {
    public static void main(String[] args) {
        Account a=new Account();
        System.out.println(a.deposit(1000));
        System.out.println(a.withdraw(1000));
        System.out.println(a.deposit(6000));
    }
}
package zhanghu;

public class Account {
        private long id;//账号
        private int password;//密码
        private String name,personID,email;//名字,身份证号码;电子邮箱,
        private double balance;
        public Account() {
            this.name="张三";
            this.balance=1000;
        }
        public Account(long id, int password, String personID, String email) {
            this.id = id;
            this.password = password;
            this.personID = personID;
            this.email = email;
        }
        public double deposit(double money){
            return this.balance+=money;
        }
        public double withdraw(double money){
            return this.balance-=money;
        }

}