interface Car {
public void tyres();
}
class BMW implements Car {
public void tyres() {
System.out.println("BMW");
}
}
class HMW implements Car {
public void tyres() {
System.out.println("HMW");
}
}
public class DependencyInjection {
private Car car;
public void draw() {
this.car.tyres();
}
public void setCar(Car car) {
this.car = car;
}
public static void tyres1(Car c) {
c.tyres();
}
public static void main(String[] args) {
Car car = new BMW();
tyres1(car);//point 1
DependencyInjection dep=new DependencyInjection();
dep.setCar(car);//point2
dep.draw();
}
}
我想澄清一下我们在第 1 点和第 2 点创建依赖注入的优势是什么。请详细解释一下,因为我是 spring 新手???