
public class 电话支付测试 {
public static void main(String[] args) {
MobilePhone mobile = new MobilePhone("15374363882", 80, 2, 30, 2);
Telephone tel = new Telephone("0797-8888888", 50, 2, 15);
mobile.pay();
mobile.display();
System.out.println("-------------------------------------------");
tel.pay();
tel.display();
}
}
interface Payable{
void pay();
}
abstract class Phone{
String code;
public Phone(String code) {
this.code = code;
}
abstract void display();
}
class MobilePhone extends Phone implements Payable{
int time;
float price;
float internetFee;
float messageFee;
public MobilePhone(String code,int time,float price,float internetFee,float messageFee) {
super(code);
this.time = time;
this.price = price;
this.internetFee = internetFee;
this.messageFee = messageFee;
}
@Override
void display() {
System.out.println("网费:¥"+ this.internetFee);
System.out.println("短信费:¥"+ this.messageFee);
System.out.println("通话费:¥"+ this.price*time);
}
@Override
public void pay() {
System.out.println("号码是:"+this.code +",本月费用合计:¥" + (price*time+internetFee+messageFee));
}
}
class Telephone extends Phone implements Payable{
int time;
float price;
float monthFee;
public Telephone(String code,int time,float price,float monthFee) {
super(code);
this.time = time;
this.price = price;
this.monthFee = monthFee;
}
@Override
void display() {
System.out.println("¥月租费:"+ this.monthFee);
System.out.println("¥通话费:"+ this.price*time);
}
@Override
public void pay() {
System.out.println("号码是:"+this.code +",本月费用合计:¥" + (price*time+monthFee));
}
}