java 接口和抽象类应用案例
案例需求:
新建类:汽车类(Car) 引擎类(Engine)
汽车类特征:汽车名称carName 汽车颜色carColor
汽车价格carPrice 引擎carEngine
汽车类行为:介绍sayHi:汽车名称 汽车颜色 汽车价格 引擎名称 引擎类型
引擎类特征:引擎名称engineName 引擎类型engineType
创建轮胎类Tire
将四个轮胎装载在汽车上 输出轮胎装到汽车上的哪个位置
左前轮 右前轮 左后轮 右后轮
轮胎类
成员变量 轮胎名称tireName 轮胎类型tireType 轮胎位置tirePosition
轮胎要按在车上 使用容器数组装
车类
/*
* 汽车类特征:汽车名称carName 汽车颜色carColor
汽车价格carPrice 引擎carEngine
汽车类行为:介绍sayHi:汽车名称 汽车颜色 汽车价格 引擎名称 引擎类型
*/
public class Car {
private String carName;
private String carColor;
private String carPrice;
//声明引擎类对象作为成员变量
private Engine engine;
//轮胎要保存在车上面 保存四个
//用容器装(数组保存)
private Tire[] tire = new Tire[4];
//构造方法
public Car() {
super();
// TODO Auto-generated constructor stub
}
public Car(String carName, String carColor, String carPrice, Engine engine) {
super();
this.carName = carName;
this.carColor = carColor;
this.carPrice = carPrice;
this.engine = engine;
}
//set/get方法
public String getCarName() {
return carName;
}
public void setCarName(String carName) {
this.carName = carName;
}
public String getCarColor() {
return carColor;
}
public void setCarColor(String carColor) {
this.carColor = carColor;
}
public String getCarPrice() {
return carPrice;
}
public void setCarPrice(String carPrice) {
this.carPrice = carPrice;
}
public Engine getEngine() {
return engine;
}
public void setEngine(Engine engine) {
this.engine = engine;
}
//轮胎数组的set/get方法
public Tire[] getTire() {
return tire;
}
public void setTire(Tire[] tire) {
this.tire = tire;
}
//装轮胎的方法 (把轮胎装到声明的数组中)
//将轮胎一个一个装到数组中
//tirePosition 表示轮胎该装在什么位置
// 相当于该放在数组的什么位置
public void setTires(Tire tire, int tirePosition) {
//把轮胎按位置放入数组中
this.tire[tirePosition] = tire;
//按照索引赋值轮胎位置信息
switch (tirePosition) {
case 0:
tire.setTirePosition("左前轮");
break;
case 1:
tire.setTirePosition("右前轮");
break;
case 2:
tire.setTirePosition("左后轮");
break;
case 3:
tire.setTirePosition("左后轮");
break;
default:
break;
}
}
//介绍方法sayHi
public void sayHi() {
//如果不给engine 赋值 初值就是null
//null调用方法 会发生空指针异常
System.out.println("引擎名称:"+engine.getEngineName()+" 引擎类型:"+engine.getEngineType());
System.out.println("车名称:"+carName+" 车颜色:"+carColor+" 车价格:"+carPrice);
//需要遍历数组 打印每一个轮胎的属性
for (int i = 0; i < this.tire.length; i++) {
//取出每一个轮胎
Tire t = this.tire[i];
System.out.println(t.toString());
}
}
}
引擎类
//引擎类特征:引擎名称engineName 引擎类型engineType
public class Engine {
private String engineName;
private String engineType;
//构造函数
public Engine() {
super();
// TODO Auto-generated constructor stub
}
public Engine(String engineName, String engineType) {
super();
this.engineName = engineName;
this.engineType = engineType;
}
//set/get方法
public String getEngineName() {
return engineName;
}
public void setEngineName(String engineName) {
this.engineName = engineName;
}
public String getEngineType() {
return engineType;
}
public void setEngineType(String engineType) {
this.engineType = engineType;
}
}
轮胎类
/*
* 轮胎类
成员变量 轮胎名称tireName 轮胎类型tireType 轮胎位置tirePosition
*/
public class Tire {
private String tireName;
private String tireType;
// 左前轮 右前轮 左后轮 右后轮
//安装时才会有位置信息 才生产时没有
//so 构造方法只需要两个参数 位置信息要装车上的时候 再进行赋值
private String tirePosition;
//构造方法
public Tire() {
super();
// TODO Auto-generated constructor stub
}
public Tire(String tireName, String tireType) {
super();
this.tireName = tireName;
this.tireType = tireType;
}
//set/get方法
public String getTireName() {
return tireName;
}
public void setTireName(String tireName) {
this.tireName = tireName;
}
public String getTireType() {
return tireType;
}
public void setTireType(String tireType) {
this.tireType = tireType;
}
public String getTirePosition() {
return tirePosition;
}
public void setTirePosition(String tirePosition) {
this.tirePosition = tirePosition;
}
//重写tostring方法
public String toString(){
String string = "轮胎名字:" + tireName + "轮胎类型:" + tireType +"轮胎位置:" + tirePosition;
return string;
}
}
测试类
public class Test {
public static void main(String[] args) {
//创建 引擎对象
Engine engine = new Engine("V16发动机", "6.0T");
//创建轮胎对象
Tire tires = new Tire();
//创建车对象
Car car = new Car("兰博基尼", "黄色", "8000000000", engine);
//循环装轮胎
for (int i = 0; i < 4; i++) {
Tire tire = new Tire("米其林","雨胎");
//将轮胎装到车上 setTire方法
car.setTires(tire, i);
}
car.sayHi();
}
}