public abstract class Transport {
abstract void distance();//创建抽象方法
public static void main(String[] args) {
// TODO 自动生成的方法存根
Car QQ=new Car();
QQ.getInfo("奇瑞",80,1.5f);
QQ.distance();
Train G=new Train();
G.getInfo("G7023",380,1.5f);
G.distance();
}
}
class Car extends Transport{//创建继承父类Transport的子类Car
String type;//定义子类Car的成员变量
float time;//定义子类Car的成员变量
int speed;//定义子类Car的成员变量
float s;//定义子类Car的成员变量
void getInfo(String t,int s,float ti){//创造方法,获取属性的值
this.type=t;
this.time=ti;
this.speed=s;
}
void distance(){//修改抽象方法distance,计算行程
s=speed*time;
System.out.println("汽车的型号:"+type);
System.out.println("汽车以时速"+speed+"千米/小时行驶"+time+"小时,行驶的路程"+s+"千米");
}
}
class Train extends Transport{//创建继承父类Transport的子类Train
String num;//定义子类Train的成员变量
float time;//定义子类Train的成员变量
int speed;//定义子类Train的成员变量
float s;//定义子类Train的成员变量
void getInfo(String n,int s,float t){//创造方法,获取属性的值
this.num=n;
this.time=t;
this.speed=s;
}
void distance(){//修改抽象方法distance,计算行程
s=speed*time;
System.out.println("火车的型号:"+num);
System.out.println("火车以时速"+speed+"千米/小时行驶"+time+"小时,行驶的路程"+s+"千米");
}
}
运行结果: