Onnnn
2017-12-06 22:14
package com.练习;
//父类Vehicle
public class Vehicle {
public int man;//载人
public String way;//在哪跑(陆地,水上,空中)
public void manned() {
System.out.println("交通工具在"+way+"能够运输 "+man+"个客人");
}
}
//子类Bus
public class Bus extends Vehicle {
int man = 50;
String way = "陆地";
public void manned() {
System.out.println("公共汽车在"+way+"能够运输 "+man+"个客人");
}
}
//子类Steamer
public class Steamer extends Vehicle {
int man = 100;
String way = "海上";
public void manned() {
System.out.println("轮船在"+way+"能够运输 "+man+"个客人");
}
}
//子类Plane
public class Plane extends Vehicle {
int man = 500;
String way = "空中";
public void manned() {
System.out.println("飞机在"+way+"能够运输 "+man+"个客人");
}
}
public class Initial {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Vehicle bus = new Bus();
Vehicle stea = new Steamer();
Vehicle plane = new Plane();
bus.manned();
stea.manned();
plane.manned();
}
}
//最后结果
公共汽车在陆地能够运输 50个客人
轮船在海上能够运输 100个客人
飞机在空中能够运输 500个客人
我觉的可以修改如下:
//父类Vehicle public class Vehicle { public void manned(String name,String way,int man) { System.out.println(name+"在"+way+"能够运输 "+man+"个客人"); } } //子类Bus public class Bus extends Vehicle { } //子类Steamer public class Steamer extends Vehicle { } //子类Plane public class Plane extends Vehicle { } //测试 public class Initial { public static void main(String[] args) { // TODO 自动生成的方法存根 Vehicle bus = new Bus(); Vehicle stea = new Steamer(); Vehicle plane = new Plane(); bus.manned("汽车","陆地上","50"); stea.manned("汽车","陆地上","50"); plane.manned("汽车","陆地上","50"); } }
这样子类可以通过直接传参,就不用重写了.
最后那个人数那里,就不用加""了吧,int类型
jjjj
嗯 是这样
Java入门第二季 升级版
530652 学习 · 6091 问题
相似问题