[b]Covariance[/b] means that [b]the type of arguments[/b], [b]return values[/b], or [b]exceptions[/b] of overriding methods can be subtypes of the original types.
在Java中不支持arguments(参数)的Covariance
override(继承) -- covariance of return value and/or exception
在继承时,允许 子类方法中“返回值和exception”是父类方法的 “返回值和exception” 的子类,这是override覆盖
在继承时,参数类型即使具有继承关系,那也是两个方法,是overload重载
override例子
overload例子
参数的Covariance,属于overload
在Java中不支持arguments(参数)的Covariance
override(继承) -- covariance of return value and/or exception
在继承时,允许 子类方法中“返回值和exception”是父类方法的 “返回值和exception” 的子类,这是override覆盖
在继承时,参数类型即使具有继承关系,那也是两个方法,是overload重载
override例子
class Parent{
Object func(Number n) throws Exception{
...
}
}
class Child extends Parent{
String func(Number n) throws SQLException {
...
}
}
overload例子
public class C {
public void func(Integer n){
}
public void func(String n){
}
参数的Covariance,属于overload
class Parent{
Object func(Number n){
...
}
}
class Child extends Parent{
Object func(Integer i) {
...
}
}
类的方法表
parent vtable
Entry 1: Object func(Number n) of Parent
child vtable
Entry 1: Object func(Number n) of Parent
Entry 2: Object func(Integer i) of Child
本文详细介绍了Java中方法的Covariance与Overload概念,特别关注了返回值和异常的继承关系以及参数类型在继承时的处理方式。通过实例分析了Covariance和Overload的区别与应用。
2409

被折叠的 条评论
为什么被折叠?



