Java基础 创建一个账户Account类,该类有id:账户号码(长整数),password:账户密码,name:真实姓名
题目:
创建一个账户Account类,该类有id:账户号码(长整数),password:账户密码,name:真实姓名,personId:身份证号码 字符串类型,email:客户的电子邮箱,balance:账户余额.方法:deposit: 存款方法,参数是double型的金额;withdraw:取款方法,参数是double型的金额.构造方法:有参和无参,有参构造方法用于设置必要的属性。
账户Account类
public class Account {
public long id;
public String password;
public String name;
public String personId;
public String email;
public double balance;
// 存钱
public double deposit(double money) {
balance += money;
return balance;
}
// 取钱
public double withdraw(double money) {
balance -= money;
return balance;
}
// 无参构造
public Account() {
}
// 有参构造
public Account(long id, String password, String name, String personId,
String email, double balance) {
this.id = id;
this.password = password;
this.name = name;
this.personId = personId;
this.email = email;
this.balance = balance;
}
}
测试类
class AccountTest {
public static void main(String[] args) {
System.out.println("----------无参构造------------");
Account account = new Account();
account.id = 1001;
account.password = "123456";
account.name = "司马懿";
account.personId = "410648023207122014";
account.email = "1881533@163.com";
account.balance = 6666.00;
testFun(account);
System.out.println("\n----------有参构造------------");
Account account1 = new Account(1002, "root", "诸葛亮",
"410648023104172014", "1545151@qq.com", 1024);
testFun(account1);
}
// 测试方法
static void testFun(Account obj) {
System.out.println("id:" + obj.id);
System.out.println("密码:" + obj.password);
System.out.println("名字:" + obj.name);
System.out.println("身份证号:" + obj.personId);
System.out.println("邮箱:" + obj.email);
System.out.println("余额" + obj.balance);
System.out.println("------无参构造方法调用--------");
System.out.println("存款后账户余额为:" + obj.deposit(1024));
System.out.println("取款后账户余额为:" + obj.withdraw(1024));
}
}
运行结果: