Humen; com.example.demo2.Car.Car; ZhangSan HumenWithCar{ (Car car){ (car); } (){ .start(); .turnLeft(); .turnRight(); .stop(); } }
Humen; com.example.demo2.Car.Car; LiSi HumenWithCar{ (Car car){ (car); } (){ .start(); .turnRight(); .stop(); } }
com.example.demo2.Car; java.lang.reflect.Constructor; java.lang.reflect.InvocationTargetException; java.util.Map; java.util.concurrent.ConcurrentHashMap; IoCContainer { Map<String, Object> = ConcurrentHashMap<String, Object>(); Object (String beanId) { .get(beanId); } (Class<?> clazz, String beanId, String ...paramBeanIds) { Object[] paramValues = Object[paramBeanIds.]; (i = ; i < paramBeanIds.; i++) { paramValues[i] = .get(paramBeanIds[i]); } Object bean = ; (Constructor<?> constructor : clazz.getConstructors()) { { bean = constructor.newInstance(paramValues); } (InstantiationException | InvocationTargetException | IllegalAccessException e) { } } (bean==) { RuntimeException(); } .put(beanId, bean); } }
com.example.demo2; Humen.LiSi; Humen.ZhangSan; Humen.humen; com.example.demo2.Car.Audi; com.example.demo2.Car.Buick; com.example.demo2.Car.IoCContainer; org.junit.; org.junit.jupiter.api.; Demo2ApplicationTests { IoCContainer = IoCContainer(); (){ .setBeans(Audi., ); .setBeans(Buick., ); .setBeans(ZhangSan., ,); .setBeans(LiSi., ,); } () { humen zhangsan = (humen) .getBean(); zhangsan.hoHome(); humen lisi = (humen) .getBean(); lisi.hoHome(); } }
java.lang.NullPointerException: Cannot invoke "Humen.humen.hoHome()" because "zhangsan" is null
humen这个类名首字母要大写