请使用面向对象的思想,实现出租车和家用轿车的信息描述。
程序运行参考效果图如下:

任务
要求:
1、 根据出租车和家用轿车的共性,抽取父类Car
属性:车的颜色color、车主姓名userName
方法:
1) 带参构造函数(参数为color和useName)
2) 创建无参无返回值得方法use(描述内容为:我是机动车!)
3) 重写equals方法,比较两个对象是否相等(比较color,userName)
2、子类taxi
属性:所属公司( company)
方法:
1) 调用父类的构造方法,完成属性赋值
2) 创建不允许重写的ride方法,描述为:出租车是所属于在*公司的。
3) 重写父类use方法,描述为:出租车是提高市民生活质量的重要条件之一
4) 重写toString方法,输出的表现形式不同(输出color,userName)
3、子类:私家车HomeCar
属性:载客数(num)
方法:
1) 带参构造方法为所有属性赋值
2) 创建无参无返回值的display方法,表现为:拥有的颜色的私家车有**座位
3) 重载要求(2)中的display方法(带参数num),描述为:家用汽车大多有**个座位
4) 要求私家车类不允许有子类
4、测试类,运行效果参照效果图:
1) 实例化2个父类对象,并传入两组相同的参数值
2) 调用父类use方法
3) 测试重写equals方法,判断两个对象是否相等
4) 实例化子类对象,并传入相关参数值
5) 调用子类ride方法和use方法
6) 测试重写toString方法,输出子类对象的信息
7) 实例化HomeCar类对象,并传入相关参数值
8) 调用子类的display和他的重载方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| package me.feihong.cartwo;
public class Car { private String color; private String userName;
public Car(){ }
public Car(String color,String userName){ this.color=color; this.userName=userName; }
public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; }
public void use() { System.out.println("我是机动车!"); }
public boolean equals(Object obj) { if(obj==null) return false; Car temp=(Car)obj; if(this.getColor().equals(temp.getColor())&&this.getUserName().equals(temp.getUserName())) return true; else return false; }
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| package me.feihong.cartwo;
public class Taxi extends Car{ private String company;
public Taxi(){ }
public Taxi(String color,String userName){ super(color,userName); }
public String getCompany() { return company; } public void setCompany(String company) { this.company = company; }
final public void ride() { System.out.println(getUserName()+"的出租车是所属于"+getCompany()+"公司的。"); }
public void use() { System.out.println("出租车是提高市民生活质量的重要条件之一"); }
public String toString() { return "taxi的信息是:"+getUserName()+"拥有一辆"+getColor()+"的出租车"; }
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| package me.feihong.cartwo;
public final class HomeCar extends Car{ private int num;
public HomeCar(){ }
public HomeCar(String color,String useName,int num){ color=getColor(); useName=getUserName(); this.num=num; }
public int getNum() { return num; } public void setNum(int num) { this.num = num; }
public void display() { System.out.println(getUserName()+"拥有的"+getColor()+"颜色的私家车,有"+getNum()+"座位"); }
public void display(int num) { System.out.println("家用汽车大多有"+getNum()+"个座位"); }
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| package me.feihong.cartwo;
public class Test { public static void main(String[] args){ Car car1=new Car("白色","Xming"); Car car2=new Car("白色","Xming");
car1.use();
boolean flag=car1.equals(car2); System.out.println("car1和car2的引用比较:"+flag);
System.out.println("=================================="); Taxi taxi=new Taxi("蓝色", "张小泉"); taxi.setCompany("长生");
taxi.ride(); taxi.use();
System.out.println(taxi.toString()); System.out.println("=================================="); HomeCar hc=new HomeCar(); hc.setColor("紫色"); hc.setUserName("孙二娘"); hc.setNum(7); hc.display(); hc.display(5); }
}
|