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));
	}
}

运行结果:
在这里插入图片描述

  • 8
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值