package day14.t1.polumorphic;
public class TestApplyPolymorphic {
public static void main(String[] args) {
Car car = new Car(); //自身类型引用指向自身类型
car.type ="小汽车";
car.speed = 120;
car.price = 200000.00;
car.brand = "BMW"; //私有属性
car.run();
Vehicle veh = new Car(); //父类引用指向子类对象
veh.type="小汽车";
veh.speed=150;
veh.price=3000000.00;
veh.run();
Bus bus = new Bus();
bus.type="公交车";
bus.speed=40;
bus.price = 1000000.00;
bus.seatNum = 20;
Bicyle bic = new Bicyle();
bic.type="自行车";
bic.speed=20;
bic.price=200.2;
bic.color="hongse";
Empoyee emp = new Empoyee();
emp.name="tom";
//emp.goHome(bus);
emp.goHome(bic);
//新内容
Empoyee emp2 = new Empoyee();
emp2.name = "jack";
Vehicle myvehicle = emp2.buyVehicle(22);
if( myvehicle != null) {
myvehicle.run();
}else {
System.out.println("没钱");
}
}
}
//员工
class Empoyee{
String name;
//回家(父类类型作为方法的形参,实现多态)
public void goHome(Vehicle veh) {
System.out.print(name + "正在回家");
veh.run();
}
//新内容
public Vehicle buyVehicle(int money) {
Vehicle veh = null;
if(money >= 100) {
Bus bus = new Bus();
bus.type="公交车";
bus.speed=50;
bus.price=1000000;
bus.seatNum=16;
veh = bus;
}else if(money >= 30) {
Car car = new Car();
car.type="小汽车";
car.speed=80;
car.price=305287;
car.brand="Bezs";
veh = car;
}else if(money >=20){
Bicyle bic = new Bicyle();
bic.type="自行车";
bic.speed=30;
bic.price=2100;
bic.color="黑色";
veh = bic;
}
return veh;
}
/*
* public void goHome(Bus bus) { System.out.print(name + "正在回家"); bus.run(); }
* public void goHome(Bicyle bic) { System.out.print(name + "正在乘坐"); bic.run();
* }
*/
}
class Vehicle{
String type;
int speed;
double price;
public void run() {
System.out.println("一辆价值 " + price + type +speed + "速度");
}
}
class Car extends Vehicle{
String brand;
public void run() {
System.out.println("一辆价值 " + price + type +speed + "速度" + brand +"品牌");
}
}
class Bus extends Vehicle{
int seatNum;
}
class Bicyle extends Vehicle{
String color;
public void run() {
System.out.println("一辆价值 " + price + color + type +speed + "速度");
}
}