Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
当重写超类的方法时,Java 允许返回类型是协变的。
为什么在重写方法时不允许使用相反的逆变参数类型?
因为这叫做重载。
特别是,返回类型类型可以是协变的,因为在重载时不考虑它,因此它仍然匹配超类或接口的实现。重载时考虑参数。您很可能与Number doSomethingWithNumber(Integer value)相比进行了优化Number doSomethingWithNumber(Number value)。
Number doSomethingWithNumber(Integer value)
Number doSomethingWithNumber(Number value)