2.请使用面向对象的思想,设计自定义类,描述出租车和家用轿车的信息。 设定
1)出租车类:
属性包括:车型,车牌,所属出租公司;方法包括:启动,停止
2)家用轿车类:
属性包括:车型,车牌,车主姓名;方法包括:启动,停止
要求
1)分析出租车和家用轿车的公共成员,提取出父类—汽车类
2)利用继承机制,实现出租车类和家用轿车类
3)编写测试类,分别测试汽车类,出租车类和家用轿车类对象的相关方法
4)定义名为 car 的包存放汽车类,出租车类,家用轿车类和测试类运行效果
先编写一个父类Car
class Car { //1)分析出租车和家用轿车的公共成员,提取出父类—汽车类
String models;
String car_id;
public Car(){
}
public Car(String models, String car_id) {
this.models = models;
this.car_id = car_id;
}
public String getModels() {
return models;
}
public void setModels(String models) {
this.models = models;
}
public String getCar_id() {
return car_id;
}
public void setCar_id(String car_id) {
this.car_id = car_id;
}
public void Initiate() {
System.out.println("我是车,我启动");
}
public void Stop() {
System.out.println("我是车,我停止");
}
}
私家车类
class Sedans extends Car {
private String name;
public Sedans(){
}
public Sedans(String models, String car_id, String name) {
super(models, car_id);
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void Initiate(){
System.out.println("我是"+name+",我的汽车我做主");
}
public void Stop(){
System.out.println("目的地到了,我们去玩吧");
}
}
出租车类
class Taxi extends Car {
public String getFirm() {
return firm;
}
public void setFirm(String firm) {
this.firm = firm;
}
private String firm;
Taxi(){
}
public Taxi(String models, String car_id, String firm) {
super(models, car_id);
this.firm = firm;
}
public void Initiate(){
System.out.println("乘客您好"+"\n"+"我是"+firm+"的,我的车牌号是"+car_id+",您要去哪里?");
}
public void Stop(){
System.out.println("目的地已经到了,请您付费下车,欢迎下次乘坐");
}
}
编写一个测试类
public class Test04 {
public static void main(String[] args) {
Car c1 = new Car();
c1.Initiate();
c1.Stop();
System.out.println("=========================");
Sedans s1=new Sedans("宝马5系","京A8888","武大郎");
s1.Stop();
s1.Initiate();
System.out.println("=========================");
Taxi t1 = new Taxi("比亚迪","京AB123","景顺出租车公司");
t1.Initiate();
t1.Stop();
}
}
标签:题目,String,继承,car,void,public,models,java,id
From: https://www.cnblogs.com/ndmtzwdx/p/18430261