举个栗子好吗
2019-05-16 02:45
public class Vehicle { int people; double price; String sp; public Vehicle(int people, double price, String sp){ this.people = people; this.price = price; this.sp = sp; } public void use(){ System.out.println("**功能** " + "载客:" + people +" 价格:" + price); System.out.println("到达目的地时间" + sp); } }
子类1:
public class Car extends Vehicle { public Car(int people, double price, String sp){ super(people, price, sp); } }
子类2:
public class Boat extends Vehicle { public Boat(int people, double price, String sp){ super(people, price, sp); } }
子类3:
public class Plane extends Vehicle { public Plane(int people, double price, String sp){ super(people, price, sp); } }
main函数:
public class Main { public static void main(String args[]){ Vehicle car = new Car(4,23.4,"4小时"); Car car2 = new Car(5,20.2,"也是4小时"); Boat boat = new Boat(100,40.0,"3小时"); Plane plane = new Plane(200,345.5,"1小时"); car.use(); car2.use(); boat.use(); plane.use(); } }
运行结果:
**功能** 载客:4 价格:23.4
到达目的地时间4小时
**功能** 载客:5 价格:20.2
到达目的地时间也是4小时
**功能** 载客:100 价格:40.0
到达目的地时间3小时
**功能** 载客:200 价格:345.5
到达目的地时间1小时
super(people, price, sp);
这个是什么意思?
相互学习相互学习。
Java入门第二季 升级版
530558 学习 · 6091 问题
相似问题