在尝试理解依赖注入原理时,我遇到了这个我无法理解的示例
abstract class ExternalInvestmentBase {
private static ExternalInvestmentBase sImpl;
protected ExternalInvestmentBase() {
sImpl = this;
}
public static String supply(String request) throws Exception {
return sImpl.supplyImpl(request);
}
abstract String supplyImpl(String request)
throws Exception;
}
class InvestmentUtil extends ExternalInvestmentBase {
public static void init() {
new InvestmentUtil();
}
@Override
public String supplyImpl(String request) throws Exception {
return "This is possible";
}
}
public class IExternalInvestment {
public static void main(String[] args) throws Exception {
InvestmentUtil.init();
String rv = ExternalInvestmentBase.supply("tt");
System.out.println(rv);
}
}
主要问题是
- 基类中的“this”关键字是如何工作的?
- 对象是如何
ExternalInvestmentBase.supply("tt");
访问的?