java 一般方法_一般覆盖Java中的方法
JLS # 8.4.2. Method Signature
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.
根據(jù)上述規(guī)則,因?yàn)槟母改笡]有擦除,您的孩子有一個(gè),所以它不是一個(gè)有效的覆蓋。
示例8.4.8.3-4。擦除影響覆蓋
類不能有兩個(gè)具有相同名稱和類型的成員方法擦除:
class C {
T id (T x) {...}
}
class D extends C {
Object id(Object x) {...}
}
這是非法的,因?yàn)镈.id(Object)是D的成員,C.id(String)被聲明為D的超類型,并且:
>這兩個(gè)方法具有相同的名稱,id
> C.id(String)可以訪問D
> D.id(Object)的簽名不是其中的一個(gè)子簽名
C.id(String)
>這兩種方法有相同的擦除
類的兩種不同方法可能不會(huì)覆蓋具有相同擦除的方法:
class C {
T id(T x) {...}
}
interface I {
T id(T x);
}
class D extends C implements I {
public String id(String x) {...}
public Integer id(Integer x) {...}
}
這也是非法的,因?yàn)镈.id(String)是D的成員,D.id(Integer)在D中聲明,并且:
>這兩個(gè)方法具有相同的名稱,id
> D.id(整數(shù))可以訪問D
>這兩種方法有不同的簽名(也不是一個(gè)
另一方的子簽)
> D.id(String)覆蓋C.id(String)和D.id(Integer)
覆蓋了I.id(整數(shù)),但是這兩個(gè)被覆蓋的方法都是一樣的
擦除
還給出了從超級(jí)到小孩允許的情況的例子
The notion of subsignature is designed to express a relationship between two methods whose signatures are not identical, but in which one may override the other. Specifically, it allows a method whose signature does not use generic types to override any generified version of that method. This is important so that library designers may freely generify methods independently of clients that define subclasses or subinterfaces of the library.
考慮一下這個(gè)例子:
class CollectionConverter {
List toList(Collection c) {...}
}
class Overrider extends CollectionConverter {
List toList(Collection c) {...}
}}
現(xiàn)在假設(shè)這個(gè)代碼是在引入泛型之前編寫的,現(xiàn)在CollectionConverter類的作者決定生成代碼,因此:
class CollectionConverter {
List toList(Collection c) {...}
}
沒有特別的分配,Overrider.toList將不再覆蓋CollectionConverter.toList。相反,代碼將是非法的。這將大大抑制泛型的使用,因?yàn)閳D書館作家會(huì)猶豫遷移現(xiàn)有的代碼。
總結(jié)
以上是生活随笔為你收集整理的java 一般方法_一般覆盖Java中的方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java stringbuffer原理_
- 下一篇: java美元兑换,(Java实现) 美元