哪位大神可以幫我看一下這樣寫可以嗎
//父類
public class Vehicle {
public int carryNum;//可載人數(shù)
public String move;//運(yùn)行方式
? ?public String models;//工具類型
? public void method(){
? System.out.println("交通工具中:"+models+"在"+move+"可以載客"+carryNum+"人");
? }
}
//公交車子類
public class Bus extends Vehicle {
public void method(){
? this.carryNum=40;
? this.move="陸地";
? this.models="公交車";
? System.out.println("交通工具中:"+models+"在"+move+"可以載客"+carryNum+"人");
? }
}
//飛機(jī)子類
public class Plane extends Vehicle{
public void method(){
? this.carryNum=200;
? this.move="天空";
? this.models="飛機(jī)";
? System.out.println("交通工具中:"+models+"在"+move+"可以載客"+carryNum+"人");
? }
}
//輪船子類
public class Ship extends Vehicle {
? public void method(){
? this.carryNum=2000;
? this.move="海洋";
? this.models="輪船";
? System.out.println("交通工具中:"+models+"在"+move+"可以載客"+carryNum+"人");
? }
}
//測(cè)試
public class Test {
public static void main(String[] args) {
Vehicle d = new Vehicle();
Vehicle d1= new Bus();
Vehicle d2= new Ship();
Vehicle d3= new Plane();
? ?d1.method();
? ?d2.method();
? ?d3.method();
}
}
2017-06-20
去看看我的那個(gè)提問,你這個(gè)寫法跟我寫的一樣,太繁瑣,而且根本沒有體現(xiàn)出繼承!
2017-07-12
也是繼承