在Vehicle类的基础上创建一个Tractor(拖拉机)类,他不大拥有journey,wheelNum,loadNum,driveSpeed 四种属性和driveAt()方法,而且还拥有自己的plough(耕地)方法,在该方法中要求能够显示journey,wheelNum,loadNum三个属性的值,在这个类中还要创建一个main()方法,分别调用plough()和driveAt方法
public class Vehicle{
public float journey;
public int wheelNum;
public int loadNum;
public int driveSpeed;
public Vehicle(){
journey=100.3f;
wheelNum=4;
loadNum=1;
}
public void driveAt(int speed){
if (speed>=60){
System.out.println("行车速度太快,容易造成事故");
//如果要求的车速太快,则机动车自动以40单位速度行驶
driveSpeed=40;
}
else {
System.out.println("你在安全行驶速度内行驶");
driveSpeed=speed;
}
}
}
參考答案:public class Tractor extends Vehicle{
Tractor(){
super();
}
public void plough(){
System.out.println(journey);
System.out.println(wheelNum);
System.out.println(loadNum);
}
public static void main(String[] args){
Tractor tractor=new Tractor();
tractor.plough();
tractor.driveAt(60);
}
}