public class Teacher {
private String name;
public void teach(){
}
}
public class University {
private List<Teacher> teacherList;
}
public class Car {
private Engine engine;
private Tyre tyre;
public Car() {
engine = new Engine();
tyre = new Tyre();
}
}
public class Car {
private Engine engine;
private List<Tyre> tyres;
// 构造方法
public Car(Engine engine, List<Tyre> tyres) {
this.engine = engine;
this.tyres = tyres;
}
// drive方法
public void drive() {
// 检查引擎和轮胎状态
if (engine == null) {
System.out.println("Cannot drive: No engine installed!");
return;
}
if (tyres == null || tyres.size() != 4) {
System.out.println("Cannot drive: Need exactly 4 tyres!");
return;
}
// 启动过程
System.out.println("Starting engine...");
System.out.println("Engine running at " + engine.getHorsepower() + " horsepower");
// 检查轮胎
System.out.println("Checking tyres...");
for (int i = 0; i < tyres.size(); i++) {
Tyre tyre = tyres.get(i);
System.out.println("Tyre " + (i+1) + ": " + tyre.getBrand() + " brand");
}
// 开始行驶
System.out.println("Car is now driving smoothly!");
}
// Getter和Setter方法保持不变
public Engine getEngine() {
return engine;
}
public void setEngine(Engine engine) {
this.engine = engine;
}
public List<Tyre> getTyres() {
return tyres;
}
public void setTyres(List<Tyre> tyres) {
this.tyres = tyres;
}
}
public class Engine {
private int horsepower;
public int getHorsepower() {
return horsepower;
}
public void setHorsepower(int horsepower) {
this.horsepower = horsepower;
}
}
public class Tyre {
private String brand;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
}