[说明]
下面程序是为汽车市场编制的一个程序的一部分。其中automobile是基类。
[Java程序]
class MainJava{
public static void main(String agr[]){
car sedan=new car();
sedan.initialize(24,20.0,4);
System.out.println ("The sedan can travel"
+ (1) +"miles.\n");
System.out.println ("The sedan has"
+ (2) +"doors.\n");
}
}
class automobile{
protected int miles_per_gallon; //汽车每加仑行驶公里数
(3) double fuel_capacity; //油箱容积
public void initialize(int in_mpg,int in_fuel){
miles_per_galion=in_mpg;
fuel_capacity=in_fuel;
}
public int get_mpg(){//提供一辆特定汽车每加仑公里数
return miles_per_gallon;
}
public double get_fuel(){//提供油箱容积
return fuel_capacity;
}
public double travel_distance(){//计算满油箱情况下的可行驶总公里数
return (4);
}
}
class car (5) {
private int Total_doors;
public void initialize(int in_mpg,double in_fuel,int doors){
Total_doors=doors;
miles_per_gallon=in_mpg;
fuel_capacity=in_fuel;
}
public int doors(){
return Total_doors;
}
}