java中的泛型的一些常見例子_第1頁(yè)
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、java中的泛型的一些常見例子public vo name(t name) = name; public v getage() return age; public void setage(v age) this.age = age; private t name; private v age; public ic void main(sing args) hello string, integer he = new hello string, integer he.setage(10); he.setname("rollen holt"); syste

2、m.out.print(he.getname() + " " + he.getage(); * author rollen-holt 泛型類的構(gòu)造辦法定義 class hello t, v hello(t name, v age) this.age = age; = name; public t getname() return name; public v getage() return age; private t name; private v age; public static void main(string args) hello stri

3、ng,integer he=new hello string,integer ("rollen",12); system.out.println(he.getname()+" "+he.getage(); * author rollen-holt 用法通配符 class info t info(t name) = name; private t name; class hello public static void fution(info ? temp) system.out.println("內(nèi)容: "+tem

4、p); public static void main(string args) info string demo=new info string ("rollen"); function(demo); * author rollen-holt 泛型上限 class info t info(t age) this.age=age; private t age; class hello public static void function(info ? tends number temp) system.out.println("內(nèi)容"+ temp);

5、public static void main(string args) info integer demo=new info integer function(demo); * author rollen-holt 泛型下限 class info t info(t age) this.age=age; private t age; class hello public static void function(info ? per string temp) system.out.println("內(nèi)容"+ temp); public static void main(st

6、ring args) / 此處只能用法string 或者object info string demo=new info string ("rollen"); function(demo); * author rollen-holt 泛型和子類繼承的限制 class info t class hello public static void main(string args) info string demo1=new info string info object demo2=new info object /demo2=demo1; 此處錯(cuò)誤 * 上面的例子解釋,一個(gè)類

7、的子類可以通過多態(tài)性被其父類實(shí)例化 * 但是在泛型操作中,子類的泛型類型是無(wú)法被其父類的泛型類型實(shí)例化的。 假如允許上面的條語(yǔ)句的話,會(huì)浮現(xiàn): exception in thread "main" java.lang.error: unresolv compilation problem: type mismatch: cannot convert from info string to info object at hello.main(hello.java:12) 泛型接口的兩種實(shí)現(xiàn): * author rollen-holt 泛型接口的實(shí)現(xiàn)1 interface in

8、fo t public void say(); / 挺直在子類之后聲明泛型 class hello t implements info t public static void main(string args) info string demo = new hello string demo.say(); public void say() system.out.println("hello"); * author rollen-holt 泛型接口的實(shí)現(xiàn)2 interface info t public void say(); / 在子類實(shí)現(xiàn)的接口中明確給出泛型類型 cl

9、ass hello implements info string public static void main(string args) info string demo = new hello(); demo.say(); public void say() system.out.println("hello"); * author rollen-holt 用法泛型通一傳入?yún)?shù)的類型 class info t private t var; public t getvar() return var; public void setvar(t var) this.var =

10、 var; public string tostring() return this.var.tostring(); class hello public static void main(string args) info string demo1=new info string info string demo2=new info string demo1.setvar("rollen"); demo2.setvar("holt"); printa(demo1, demo2); / 此處傳遞的都是同一個(gè)string類型的 public static

11、t void printadd(info t demo1, info t demo2) system.out.println(demo1.getvar()+" "+demo2.getvar(); 否則的話如下所示:浮現(xiàn)錯(cuò)誤: * author rollen-holt 用法泛型通一傳入?yún)?shù)的類型 class info t private t var; public t getvar() return var; public void setvar(t var) this.var = var; public string tostring() return this.var.t

12、ostring(); class hello public static void main(string args) info string demo1=new info string info integer demo2=new info integer demo1.setvar("rollen"); demo2.setvar(30); /此處調(diào)用錯(cuò)誤 printadd(demo1, demo2); / 此處傳遞的都是同一個(gè)string類型的 public static t void printadd(info t demo1, info t demo2) system.out.println(demo1.getvar()+" "+demo2.getvar(); exception in thread "main" java.lang.error

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論