设计三个类:银行账户类、智能手机类、测试类。 在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);
}
}