1、Vehicle类所具有的属性题目没有给定数据类型,自主选择定义属性的数据类型。代码如下
public class Vehicle{
private double speed;//速度
private String name;//名字
private String color;//颜色
public double getSpeed() {
return speed;
}
public void setSpeed(double speed) {
this.speed = speed;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
//start方法
public void start()
{
System.out.println("汽车,"+this.name+this.color+"启动。");
}
//stop方法
public void stop( )
{
System.out.println("汽车,"+this.name+this.color+"停止。");
}
//run方法
public void run( )
{
System.out.println("汽车,"+this.name+"行驶,速度 "+this.speed+"km/h");
}
public static void main(String[] args) {
//主方法,执行需要的操作
Vehicle v =new Vehicle();
v.setName("奔驰");
v.setColor("红色");
v.setSpeed(120);
v.start();
v.run();
v.stop();
}
}
2、在主方法程序中生成Vehicle的对象,通过set方法给对象赋值,运行效果
public abstract class Vehicle{
public int speed;
public String name;
public String color;
public void start(){}
public void stop(){}
public void run(){}
}
public class Aircraft extends Vehicle{}
public class SpaceShuttle extends Aircraft{}
public class Jet extends Aircraft{}