设置一个抽象父类,一个继承的子类
public abstract class Parent {
protected abstract void log();
protected void use(){
log();
}
@Test
public void test(){
use();
}
}
public class Children extends Parent {
@Override
protected void log() {
System.out.println("Children...");
}
}
此时输出结果为 Children…
例如Spring创建IOC容器时
ClassPathXmlApplicationContext继承了AbstractApplicationContext
public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
this(new String[] {configLocation}, true, null);
}
public ClassPathXmlApplicationContext(
String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
throws BeansException {
super(parent);
setConfigLocations(configLocations);
if (refresh) {
refresh(); //此为AbstractApplicationContext的方法
}
}
所以调用AbstractApplicationContext的refresh()方法刷新beanFactory时,就会调用该抽象类的子类AbstractRefreshableApplicationContext的refreshBeanFactory()以及getBeanFactory()方法