(1)要求至少设计三个类,一个座机类,一个移动手机类,一个测试类,在测试类中需应用自己已经创建的功能模块的方法。 (2)基本功能应包括:可以根据通话时长收取相应的费用,同时具有充话费、显示余额的功能。用java程序运行上面描述的代码
时间: 2024-04-01 18:32:17 浏览: 10
好的,以下是Java实现的代码,仅供参考:
```java
public class Phone {
private String phone_number;
private double balance;
public Phone(String phone_number, double balance) {
this.phone_number = phone_number;
this.balance = balance;
}
public void charge(double amount) {
this.balance += amount;
}
public double check_balance() {
return this.balance;
}
}
public class LandlinePhone extends Phone {
private double call_rate;
private int call_duration;
public LandlinePhone(String phone_number, double balance, double call_rate) {
super(phone_number, balance);
this.call_rate = call_rate;
this.call_duration = 0;
}
public double make_call(int duration) {
this.call_duration += duration;
double cost = this.call_duration * this.call_rate;
this.balance -= cost;
return cost;
}
}
public class MobilePhone extends Phone {
private int data_usage;
private String package_info;
public MobilePhone(String phone_number, double balance, int data_usage, String package_info) {
super(phone_number, balance);
this.data_usage = data_usage;
this.package_info = package_info;
}
public void use_data(int amount) {
this.data_usage += amount;
}
public String check_package() {
return this.package_info;
}
}
public class TestPhone {
public void testLandlinePhone() {
LandlinePhone lp = new LandlinePhone("010-12345678", 10, 0.5);
assert lp.make_call(5) == 2.5;
assert lp.check_balance() == 7.5;
}
public void testMobilePhone() {
MobilePhone mp = new MobilePhone("13912345678", 10, 0, "100M");
mp.use_data(20);
assert mp.check_balance() == 10;
assert mp.check_package() == "100M";
}
public void testPhone() {
Phone p = new Phone("010-12345678", 10);
p.charge(5);
assert p.check_balance() == 15;
}
public static void main(String[] args) {
TestPhone tester = new TestPhone();
tester.testLandlinePhone();
tester.testMobilePhone();
tester.testPhone();
}
}
```
在上面的代码中,Phone 类表示所有手机类的基类,包含通用的属性和方法,如电话号码、余额、充话费、查询余额等。LandlinePhone 类继承自 Phone 类,表示座机类,包含座机特有的属性和方法,如通话时长、收费标准等。MobilePhone 类也继承自 Phone 类,表示移动手机类,包含移动手机特有的属性和方法,如流量用量、套餐信息等。
TestPhone 类则是测试类,其中包含了三个测试方法,分别测试座机类、移动手机类和基础手机类的功能是否正常。在每个测试方法中,首先创建相应的对象,然后调用对象的方法进行测试,最后使用 assert 语句判断测试结果是否符合预期。
当然,以上只是一个简单的示例代码,实际代码应当根据具体需求进行修改和完善。