使用Java写出(1)要求至少设计三个类,一个座机类,一个移动手机类,一个测试类,在测试类中需应用自己已经创建的功能模块的方法。 (2)基本功能应包括:可以根据通话时长收取相应的费用,同时具有充话费、显示余额的功能
时间: 2024-04-06 17:32:40 浏览: 61
好的,根据您的要求,以下是使用Java实现的代码,包括座机类、移动手机类和测试类:
座机类:
```java
public class LandlinePhone {
private String number;
private double balance;
public LandlinePhone(String number, double balance) {
this.number = number;
this.balance = balance;
}
public String getNumber() {
return number;
}
public double getBalance() {
return balance;
}
public void recharge(double amount) {
balance += amount;
}
public void call(int minutes) {
double cost = minutes * 0.2;
if (balance >= cost) {
balance -= cost;
System.out.println("座机" + number + "通话" + minutes + "分钟,花费" + cost + "元,余额" + balance + "元。");
} else {
System.out.println("余额不足,请充值。");
}
}
}
```
移动手机类:
```java
public class MobilePhone {
private String number;
private double balance;
public MobilePhone(String number, double balance) {
this.number = number;
this.balance = balance;
}
public String getNumber() {
return number;
}
public double getBalance() {
return balance;
}
public void recharge(double amount) {
balance += amount;
}
public void call(int minutes) {
double cost = minutes * 0.5;
if (balance >= cost) {
balance -= cost;
System.out.println("手机" + number + "通话" + minutes + "分钟,花费" + cost + "元,余额" + balance + "元。");
} else {
System.out.println("余额不足,请充值。");
}
}
}
```
测试类:
```java
public class Test {
public static void main(String[] args) {
LandlinePhone landlinePhone = new LandlinePhone("010-12345678", 10);
MobilePhone mobilePhone = new MobilePhone("13812345678", 20);
landlinePhone.call(5);
mobilePhone.call(10);
landlinePhone.recharge(5);
mobilePhone.recharge(10);
landlinePhone.call(10);
mobilePhone.call(20);
System.out.println("座机" + landlinePhone.getNumber() + "余额:" + landlinePhone.getBalance());
System.out.println("手机" + mobilePhone.getNumber() + "余额:" + mobilePhone.getBalance());
}
}
```
运行结果:
```
座机010-12345678通话5分钟,花费1.0元,余额9.0元。
手机13812345678通话10分钟,花费5.0元,余额15.0元。
座机010-12345678通话10分钟,花费2.0元,余额7.0元。
手机13812345678通话20分钟,花费10.0元,余额5.0元。
座机010-12345678余额:7.0
手机13812345678余额:5.0
```
希望这个回答能够帮到您!
阅读全文