完善下列代码class MobileShop { 【代码1】 //用内部类InnerPurchaseMoney声明对象purchaseMoney1 【代码2】 //用内部类InnerPurchaseMoney声明对象purchaseMoney2 private int mobileAmount; //手机的数量 MobileShop(){ 【代码3】 //创建价值为20000的purchaseMoney1 【代码4】 //创建价值为10000的purchaseMoney2 } void setMobileAmount(int m) { mobileAmount = m; } int getMobileAmount() { return mobileAmount; } class InnerPurchaseMoney { int moneyValue; InnerPurchaseMoney(int m) { moneyValue = m; } void buyMobile() { if(moneyValue>=20000) { mobileAmount = mobileAmount-6; System.out.println("用价值"+moneyValue+"的内部购物卷买了6部手机"); } else if(moneyValue<20000&&moneyValue>=10000) { mobileAmount = mobileAmount-3; System.out.println("用价值"+moneyValue+"的内部购物卷买了3部手机"); } } } }public class NewYear{ public static void main(String args[]) { MobileShop shop = new MobileShop(); shop.setMobileAmount(3
时间: 2024-03-16 18:46:11 浏览: 72
下面是完整代码,包括代码1、代码2、代码3和代码4:
```
class MobileShop {
InnerPurchaseMoney purchaseMoney1; // 代码1:用内部类InnerPurchaseMoney声明对象purchaseMoney1
InnerPurchaseMoney purchaseMoney2; // 代码2:用内部类InnerPurchaseMoney声明对象purchaseMoney2
private int mobileAmount; // 手机的数量
MobileShop() {
purchaseMoney1 = new InnerPurchaseMoney(20000); // 代码3:创建价值为20000的purchaseMoney1
purchaseMoney2 = new InnerPurchaseMoney(10000); // 代码4:创建价值为10000的purchaseMoney2
}
void setMobileAmount(int m) {
mobileAmount = m;
}
int getMobileAmount() {
return mobileAmount;
}
class InnerPurchaseMoney {
int moneyValue;
InnerPurchaseMoney(int m) {
moneyValue = m;
}
void buyMobile() {
if (moneyValue >= 20000) {
mobileAmount = mobileAmount - 6;
System.out.println("用价值" + moneyValue + "的内部购物卷买了6部手机");
} else if (moneyValue < 20000 && moneyValue >= 10000) {
mobileAmount = mobileAmount - 3;
System.out.println("用价值" + moneyValue + "的内部购物卷买了3部手机");
}
}
}
}
public class NewYear {
public static void main(String args[]) {
MobileShop shop = new MobileShop();
shop.setMobileAmount(30);
System.out.println("原始手机数量:" + shop.getMobileAmount());
shop.purchaseMoney1.buyMobile();
System.out.println("购买后手机数量:" + shop.getMobileAmount());
shop.purchaseMoney2.buyMobile();
System.out.println("购买后手机数量:" + shop.getMobileAmount());
}
}
```
注:为了方便测试,在main函数中添加了一些代码来演示购买手机的过程。你可以根据实际需求修改。
阅读全文