g方法解析
注意f 是一个实例方法,而g 是一个静态方法。静态方法与class关联。
因此,因为您将b1、a1 和a2 声明为A,所以调用b1.g()、a1.g() 和a2.g() 都会调用A.g()。
这是关于 Java 静态方法的另一个很好的 SO 问题:Why doesn't the compiler complain when I try to override a static method?
f方法解析
Java 实例方法默认为virtual 方法,可以被子类覆盖。所以f()实际上做了什么取决于实例是什么(即取决于new关键字后面的类型)。
b1是B的一个实例,所以b1.f()会调用B上定义的f方法,即B.f()。
i1也是B的一个实例,所以i1.f()也会调用B上定义的f方法,即B.f()。
i2是A的一个实例,所以i2.f()会调用A上定义的f方法,即A.f()。
那么a2.f()呢?如上所述,a2 是B 的一个实例(您使用new 关键字创建它),因此a2.f() 将调用B.f()。
更多关于方法覆盖
您可能想知道为什么它们会这样工作?为什么a2.f() 不是设计为在Java 中调用A.f()?原因如下:
假设您有一个模块可以对记录列表进行复杂计算。这些记录可能是从数据库中检索到的,也可能是从第三方 rest api 提供的 web api 中检索到的。如何让你的模块尽可能的可扩展?
这是你的模块代码:
public class MyModule {
public void doComplexWork() {
// Where to get the records?
List<Record> records = ???;
// Do complex computation here
}
}
一种可扩展的方法是将接口(或抽象类)定义为“记录检索”的契约:
public interface IRecordRetriever {
List<Record> retrieveRecords();
}
然后你实现两个IRecordRetrievers,一个从数据库中检索记录,而另一个从rest api中检索记录。
public class DatabaseRecordRetriever implements IRecordRetriever {
@Override
public List<Record> retrieveRecords() {
// Connect database and fetch database records here
}
}
public class RestApiRecordRetriever implements IRecordRetriever {
@Override
public List<Record> retrieveRecords() {
// Access the rest api to fetch records here
}
}
现在让我们稍微修改一下我们的模块代码:
public class MyModule {
private IRecordRetriever _recordRetriever;
// Inject IRecordRetriever from contructor
public MyModule(IRecordRetriever retriever) {
_recordRetriever = retriever;
}
public void doComplexWork() {
// Where to get the records?
// From the IRecordRetriever
List<Record> records = _recordRetriever.retrieveRecords();
// Do complex computation here
}
}
现在我们的MyModule 可以扩展了。因为无论从哪里检索记录,MyModule 中的代码都不需要更改。如果现在您从数据库中检索记录,但后来您决定更改为从rest api 检索记录,那么MyModule 中的任何内容都不需要更改!您只需要更改传递给MyModule 构造函数的运行时IRecordRetriever 实例。
为什么我们可以实现这种可扩展性?这是因为方法覆盖。我们将私有字段_recordRetriever 声明为IRecordRetriever,因此实际行为取决于实际运行时实例(由new 关键字创建)。因此,要更改记录检索行为,我们只需更改传递给MyModule 构造函数的运行时IRecordRetriever 实例。如果方法覆盖不可用,那就是:
IRecordRetriever 检索器 = new DatabaseRecordRetriever();
检索器.retrieveRecords();
如果对retriever.retrieveRecords()的调用不是调用DatabaseRecordRetriever上定义的retrieveRecords()方法,而是调用IRecordRetriever上定义的retrieveRecords()方法,我们无法实现扩展性。 (当然,IRecordRetriever 是一个接口,所以你不能调用IRecordRetriever.retrieveRecords(),但这也适用于虚方法)。
我上面提到的内容也适用于其他面向对象的编程语言。
我通过构造函数将IRecordRetriever 传递给MyModule 的方式称为Constructor Dependency Injection。您可以阅读更多有关面向对象编程、方法覆盖和 GoF 设计模式的资料。