點墨_痕
2017-06-18 10:52
//父类
package traffic;
public class Vehicle {
int Pop ;//运输工具的可载人数
String Move;//运输方式
String Models;//工具类型
public void moveCar(){
System.out.println(Models + "在" + Move +"一次可运输:" + Pop + "人");//创建方法 交通工具可以干什么
}
}
//子类 汽车
package traffic;
public class Car extends Vehicle {
public void moveCar(){
Pop = 5;
Move = "陆地";
Models = "汽车";
System.out.println(Models + "在" + Move +"一次可运输:" + Pop + "人");//创建方法 交通工具可以干什么
}
}
//子类 飞机
package traffic;
public class Aircraft extends Vehicle {
public void moveCar(){
Pop = 50;
Move = "天空";
Models = "飞机";
System.out.println(Models + "在" + Move +"一次可运输:" + Pop + "人");//创建方法 交通工具可以干什么
}
}
//子类飞机
package traffic;
public class Ship extends Vehicle {
public void moveCar(){
Pop = 50;
Move = "海上";
Models = "轮船";
System.out.println(Models + "在" + Move +"一次可运输:" + Pop + "人");//创建方法 交通工具可以干什么
}
}
//演示
package traffic;
public class AppliCation {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Vehicle traFFic = new Vehicle();
Vehicle carModel = new Car();
Vehicle carMode2 = new Aircraft();
Vehicle carMode3 = new Ship();
carModel.moveCar();
carMode2.moveCar();
carMode3.moveCar();
}
}
求大神看看这么写对不对,另外我感觉这种不方便,如果想要随时往里面传参数,应该怎么写?有没有更优化的方法让在下参考下?
你既然每个都继承了Vehicle,就直接调用方法啊,没必要每个都去重写啊,你这样的继承毫无意义啊,还不如每个单独写。
public class Ship extends Vehicle {
public Ship(String Models,String Move,int Pop){
this.Models = Models;
this.Move = Move;
this.Pop = Pop;
}
}
我们只需要给不同的运输方式赋不同的值就行了。
package com.shellinfor; //父类 public class Vehicle { int pop; String move; String models; public void movePeople(){ System.out.println(models +",是属于" + move +",可以载客" + pop+ "!"); } }
package com.shellinfor; //汽车子类 public class Car extends Vehicle { public Car(String models,String move,int pop){ this.models = models; this.move = move; this.pop = pop; } }
package com.shellinfor; //轮船子类 public class Ship extends Vehicle { public Ship(String models,String move,int pop){ this.models = models; this.move = move; this.pop = pop; } }
package com.shellinfor; //飞机子类 public class Plane extends Vehicle { public Plane(String models,String move,int pop){ this.models = models; this.move = move; this.pop = pop; } }
package com.shellinfor; //主方法类 public class Demo { public static void main(String[] args) { Vehicle ve1 = new Car("汽车", "陆运", 40); Vehicle ve2 = new Ship("轮船", "海运", 200); Vehicle ve3 = new Plane("飞机", "空运", 400); ve1.movePeople(); ve2.movePeople(); ve3.movePeople(); } }
请大神看一下,这样可以吗?还可以优化吗?
//父类package traffic; class Vehicle { //变量名第一个字母不用大写,类名或接口名第一个字母才要大写 int pop ;//运输工具的可载人数 String move;//运输方式 String models;//工具类型 public Vehicle() { } public Vehicle(int pop, String move, String models) { this.pop = pop; this.move = move; this.models = models; } public void moveCar(){ System.out.println(models + "在" + move +"一次可运输:" + pop + "人");//创建方法 交通工具可以干什么 } } //子类 汽车 class Car extends Vehicle { public Car(int pop, String move, String models){ super(pop, move, models); } } //子类 飞机 class Aircraft extends Vehicle { public Aircraft(int pop, String move, String models) { super(pop, move, models); } } //子类飞机 class Ship extends Vehicle { public Ship(int pop, String move, String models){ super(pop, move, models); } } //演示 public class AppliCation { //创建一个静态方法,利用多态,调用Vehicle子类中各自的moveCar()方法 public static void show(Vehicle v) { v.moveCar(); } public static void main(String[] args) { Vehicle car = new Car(5, "陆地", "汽车"); Vehicle aircraft = new Aircraft(50, "天空", "飞机"); Vehicle ship = new Ship(50, "海上", "轮船"); show(car); show(aircraft); show(ship); } }
//父类 package traffic; public class Vehicle { int Pop ;//运输工具的可载人数 String Move;//运输方式 String Models;//工具类型 public void moveCar(int pop, String move, String models){ this.Pop = pop; this.Move = move; this.Models = models; System.out.println(Models + "在" + Move +"一次可运输:" + Pop + "人");//创建方法 交通工具可以干什么 } } //子类 飞机 package traffic; public class Aircraft extends Vehicle { public void moveCar(){ /* Pop = 50; Move = "天空"; Models = "飞机"; System.out.println(Models + "在" + Move +"一次可运输:" + Pop + "人");//创建方法 交通工具可以干什么 */ } } //子类 汽车 package traffic; public class Car extends Vehicle { public void moveCar(){ /* Pop = 5; Move = "陆地"; Models = "汽车"; System.out.println(Models + "在" + Move +"一次可运输:" + Pop + "人");//创建方法 交通工具可以干什么 */ } } //子类 轮船 package traffic; public class Ship extends Vehicle { public void moveCar(){ /* Pop = 50; Move = "海上"; Models = "轮船"; System.out.println(Models + "在" + Move +"一次可运输:" + Pop + "人");//创建方法 交通工具可以干什么 */ } } //main package traffic; public class AppliCation { public static void main(String[] args) { // TODO Auto-generated method stub Vehicle carModel = new Car(); Vehicle carMode2 = new Aircraft(); Vehicle carMode3 = new Ship(); carModel.moveCar(5, "陆地", "汽车"); carMode2.moveCar(50, "天空", "飞机"); carMode3.moveCar(50, "海上", "轮船"); } }
我也是初学者,稍微的改了一下,不知道是不是你想要的效果
Java入门第二季 升级版
530652 学习 · 6091 问题
相似问题