日期:2014-05-20 浏览次数:20857 次
//Inheritance.java class Telephone { String brand,number; double dialledTime; double rate; Telephone(String b, String n) { //构造方法 brand = b; number = n; } String getBrand() { return brand; } String getNumber() { return number; } double getDialledTime() { return dialledTime; } double getRate() { return rate; } void setBrand(String b) { brand = b; } void setNumber(String n) { number = n; } void setDialledTime(double d) { dialledTime = d; } void setRate(double r) { rate = r; } double callCost() { return rate*dialledTime; } void display() { System.out.println("电话品牌:"+brand+" 电话号码:"+number); System.out.println("通话时间:"+dialledTime+" 费率:"+rate); System.out.println("话费:"+callCost()); } } class Mobilephone extends Telephone { String network; double receivedTime; Mobilephone(String b, String num, String net) { super(b,num); //显式的调用超类的有参构造函数 network = net; } String getNetwork() { return network; } double getReceivedTime() { return receivedTime; } void setNetwork(String n) { network = n; } void setReceivedTime(double d) { receivedTime = d; } double callCost() { return (dialledTime+0.5*receivedTime)*rate; } //方法重写 void display() { System.out.println("电话品牌:"+getBrand()+" 电话号码:"+getNumber()+" 网络:"+getNetwork()); System.out.println("主叫时间:"+getDialledTime()+" 被叫时间:"+getReceivedTime()+" 费率:"+getRate()); System.out.println("话费:"+callCost()); } } public class Inheritance { public static void main(String args[]) { Telephone tel = new Telephone("TCL","123456789"); Mobilephone mobile = new Mobilephone("Nokia","15019905753","CDMA"); tel.setRate(0.2); tel.setDialledTime(150); mobile.setRate(0.39); mobile.setDialledTime(80); mobile.setReceivedTime(120); tel.display(); System.out.println(); mobile.display(); } }