日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

java 一般方法_一般覆盖Java中的方法

發(fā)布時(shí)間:2025/3/20 java 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 一般方法_一般覆盖Java中的方法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。