以下Java代码不能编译出错:名称冲突。Java中的void f(Class c)和void f(Class <?> c)之间是否有区别?
class Test {
public void f(Class<?> c) {
}
public void f(Class c) {
}
}
有void f(Class c)
和Java无f(Class<?> c)
之间有什么区别?
以下Java代码不能编译出错:名称冲突。Java中的void f(Class c)和void f(Class <?> c)之间是否有区别?
class Test {
public void f(Class<?> c) {
}
public void f(Class c) {
}
}
有void f(Class c)
和Java无f(Class<?> c)
之间有什么区别?
声明在同一个类中,它们是覆盖等效的,并且会导致编译错误。
It is a compile-time error to declare two methods with override-equivalent signatures in a class.
其中
Two method signatures m1 and m2 are override-equivalent iff either m1 is a subsignature of m2 or m2 is a subsignature of m1.
和
The signature of a method m1 is a subsignature of the signature of a method m2 if either:
- m2 has the same signature as m1, or
- the signature of m1 is the same as the erasure (§4.6) of the signature of m2.
加粗的情况下,这里的问题。
Class<?>
的删除是Class
。
Is there any difference between void f(Class c) and void f(Class c) in Java?
从呼叫者的角度来看,没有。在该方法的主体中,是的。在第一种情况下,该参数具有raw typeClass
。在第二种情况下,该参数具有参数化类型Class<?>
。
其实?
是一个可以与参数化类/接口一起使用的通配符。例如,Collection<Object>
是一个通用集合,其中包含Object
类型的项目,而Collection<?>
是所有类型集合的超类型。
从语义上讲,一般的和明显的野生的,另一种非通用的 – raptortech97