问答详情
源自:10-1 Java 中的多态

源码,没有进行输入限制

public class Test1 {

public static void main(String[] args) {

TransForm[] tra = new TransForm[3];

tra[0] = new Plant("飞机","天空",300);

tra[1] = new Ship("船", "水上", 100);

tra[2] = new Bus("大巴", "路面", 30);

for(TransForm i : tra){

i.print();

}

}

}

/*

 * 飞机,轮船,大巴,运输人的数量和方式不同

 */

abstract class TransForm{

protected String name;

protected String ways;

protected int num;

public TransForm(){}

public TransForm (String name,String ways,int num) {

this.name = name;

this.num = num;

this.ways = ways;

}

public void print(){

System.out.println(this.name+"可以在"+this.ways+"运输"+this.num+"人");

}

}

class Plant extends TransForm{

public Plant (String name,String ways,int num) {

super(name,ways,num);

}

}


class Ship extends TransForm{

private String ways = "water";

public Ship(String name,String ways,int num) {

super(name,ways,num);

}

}

class Bus extends TransForm{

public Bus(String name,String ways,int num) {

super(name,ways,num);

}

}


提问者:嘘___________别说话 2018-08-01 22:00

个回答

  • Passionate1
    2018-08-02 08:14:43
    已采纳

    main方法哪去了

  • 嘘___________别说话
    2018-08-03 17:56:08

    https://img.mukewang.com/5b6426a00001bb3702130096.jpgtest下面