java-设计三个类:银行账户类、智能手机类、测试类。

设计三个类:银行账户类、智能手机类、测试类。 在main函数中,完成下列功能: (1) 创建一个银行账户对象    银行名称:中国银行    银行账户:1001    用户姓名:马富    账户余额:100.0 (2) 创建一个智能手机对象    电话号码:13900010001    银行账户:在(1)中创建的账户 (3) 银行账户充值30元 (4) 手机扫码支付20元 (5) 输出银行账户的余额 

/**
 * 银行账户类
 *
 */
public class Account {
	String bank;//银行名称
	String id;//银行账户
	String name;//用户姓名
	float balance;//余额
	public Account(String bank, String id, String name, float balance) {
		this.bank = bank;
		this.id = id;
		this.name = name;
		this.balance = balance;
	}
	/*
	 * 支付
	 */
	void pay (float amount) {
		balance=balance-amount;
	}
	/*
	 * 充值
	 */
	void recharge(float amount) {
		balance+=amount;
	}
}
/**
 * 智能手机类
 *
 */
public class SmartMobile {
	String phoneNumber;//电话号码
	Account account;//绑定的银行账户
	SmartMobile(String phoneNumber,Account account){
		this.phoneNumber=phoneNumber;
		this.account=account;
	}
	void scanPay(float amount) {
		account.pay(amount);
	}
}

/*
 * 测试类
 */
public class TestAccount {

	public static void main(String[] args) {
		Account account= new Account("中国银行","1001","马富",100.0f);
		SmartMobile mobile=new SmartMobile("13900010001",account);
		account.recharge(30);
		mobile.scanPay(20);
		System.out.println(account.balance);
	}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值