AutoCAD 3DMAX C語言 Pro/E UG JAVA編程 PHP編程 Maya動畫 Matlab應用 Android
Photoshop Word Excel flash VB編程 VC編程 Coreldraw SolidWorks A Designer Unity3D
 首頁 > JAVA編程

Java 泛型的理解與等價實現

51自學網 2015-09-03 http://www.0532cf.com
  泛型Java SE 1.5的新特性,泛型的本質是參數化類型,也就是說所操作的數據類型被指定為一個參數。這種參數類型可以用在類、接口和方法的創建中,分別稱為泛型類、泛型接口、泛型方法。

  Java語言引入泛型的好處是安全簡單。

  在Java SE 1.5之前,沒有泛型的情況的下,通過對類型Object的引用來實現參數的“任意化”,“任意化”帶來的缺點是要做顯式的強制類型轉換,而這種轉換是要求開發者對實際參數類型可以預知的情況下進行的。對于強制類型轉換錯誤的情況,編譯器可能不提示錯誤,在運行的時候才出現異常,這是一個安全隱患。

  泛型的好處是在編譯的時候檢查類型安全,并且所有的強制轉換都是自動和隱式的,提高代碼的重用率。

  泛型在使用中還有一些規則和限制:

  1、泛型的類型參數只能是類類型(包括自定義類),不能是簡單類型。

  2、同一種泛型可以對應多個版本(因為參數類型是不確定的),不同版本的泛型類實例是不兼容的。

  3、泛型的類型參數可以有多個。

  4、泛型的參數類型可以使用extends語句,例如<T extends superclass>。習慣上成為“有界類型”。

  5、泛型的參數類型還可以是通配符類型。例如Class<?> classType = Class.forName(java.lang.String);

  泛型還有接口、方法等等,內容很多,需要花費一番功夫才能理解掌握并熟練應用。在此給出我曾經了解泛型時候寫出的兩個例子(根據看的印象寫的),實現同樣的功能,一個使用了泛型,一個沒有使用,通過對比,可以很快學會泛型的應用,學會這個基本上學會了泛型70%的內容。

  例子一:使用了泛型

public class Gen<T> {
 private T ob; //定義泛型成員變量

 public Gen(T ob) {
  this.ob = ob;
 }

 public T getOb() {
  return ob;
 }

 public void setOb(T ob) {
  this.ob = ob;
 }

 public void showTyep() {
  System.out.println("T的實際類型是: " + ob.getClass().getName());
 }
}

public class GenDemo {
 public static void main(String[] args){
 //定義泛型類Gen的一個Integer版本
 Gen<Integer> intOb=new Gen<Integer>(88);
 intOb.showTyep();
 int i= intOb.getOb();
 System.out.println("value= " + i);

 System.out.println("----------------------------------");

 //定義泛型類Gen的一個String版本
 Gen<String> strOb=new Gen<String>("Hello Gen!");
 strOb.showTyep();
 String s=strOb.getOb();
 System.out.println("value= " + s);
}
}

  例子二:沒有使用泛型

public class Gen2 {
 private Object ob; //定義一個通用類型成員

 public Gen2(Object ob) {
  this.ob = ob;
 }

 public Object getOb() {
  return ob;
 }

 public void setOb(Object ob) {
  this.ob = ob;
 }

 public void showTyep() {
  System.out.println("T的實際類型是: " + ob.getClass().getName());
 }
}

public class GenDemo2 {
 public static void main(String[] args) {
  //定義類Gen2的一個Integer版本
  Gen2 intOb = new Gen2(new Integer(88));
  intOb.showTyep();
  int i = (Integer) intOb.getOb();
  System.out.println("value= " + i);

  System.out.println("----------------------------------");

  //定義類Gen2的一個String版本
  Gen2 strOb = new Gen2("Hello Gen!");
  strOb.showTyep();
  String s = (String) strOb.getOb();
  System.out.println("value= " + s);
 }
}

  運行結果:

  兩個例子運行Demo結果是相同的,控制臺輸出結果如下:

  T的實際類型是:

java.lang.Integer
value= 88
----------------------------------
T的實際類型是: java.lang.String
value= Hello Gen!

Process finished with exit code 0

  看明白這個,以后基本的泛型應用和代碼閱讀就不成問題了。

 

 

 
說明
:本教程來源互聯網或網友上傳或出版商,僅為學習研究或媒體推廣,51zixue.net不保證資料的完整性。
 
上一篇:從網頁源文件中得到鏈接  下一篇:Java編程技術中漢字問題的分析及解決
彩客网 www.0422000.com | 3178jj.com | www.x063801.com | 22hh8332.com | www.22288806.com | www.2109x.com | 11005l.com | www.7892.ag | 3018dd.com | www.76060z.com | www.85144aa.com | 112n.net | www.8313k.com | 88p1.cc | www.r30226.com | www.657zf.com | 3559i.com | www.1754s.com | 996622xx.com | www.byc06.com | www.c9667.com | zhcp33.com | www.0194002.com | 3657.cc | www.bwinyz05.com | 32x1.com | www.89897.com | hg88814.com | www.js3451.bet | www.hqcp5.com | ok8381.com | www.880148.com | feicai0575.com | www.608568.com | 1449b.com | www.1138js.com | www.916720.com | 0033b.cc | www.33112h.com | 3685x.com | www.587819.com | 9679l.com | www.vns0988.com | 3614p.com | www.hnhdkm.com | m7454.com | www.w8586.com | www.113438.com | www.56520c.com | www.606255.com | www.876445.com | www.089c.cc | 0080d.com | www.2632f.com | 588qqq.cc | www.07949.com | ddcp77.com | www.89777w.com | 7720c.com | www.86267f.com | jinguanylc9.com | www.954321g.com | 84490.com | www.2221104.com | 22nn8331.com | www.yi636.com | 1434x.com | www.if57.com | www.866918.com | www.321676.com | www.6suncity.com | betke.cc | www.808888s.com | feicai0991.com | www.00772v.com | 6668449.com | www.3032ll.com | mg4151.vip | www.amjs339.com | 4025m.com | www.07163e.com | 2677qqq.com | www.lczg8.com | www.21365gg.com | www.032823.com | www.414266.com | 2329.com | www.42070010.com | 7779t.cc | www.862366.com | www.4972ee.com | tzvip2025.com | www.662889.com | 88463gong.com | www.4196n.com | 4018ff.com | www.66332t.com | 6146o.com | www.332092.com | www.277577.com | 2229tz.com | www.742988.com | jbb71.cc | www.715033.com | www.9949f.com | miyaonv.com | www.63606h.com | 00442007.com | www.769211.com | www.585kk.com | amam8.com | www.hg77033.com | 518cp-4.com | www.470680.com | www.zr55555.com | 27365e.com | www.4102w.com | 253503.com | www.356686.com | www.5f33.com | 4255jjj.com | www.33598d.com | www.hg0095.com | 518cp-1.com | www.33111166.com | 6830y.com | www.262918.com | www.hd779.com | 059dhw.com | www.16878q.com | www.2078t.com | 2709h.com | www.68993228.com | www.v2808.com | 6423m.com | www.634331.com | www.38200g.com | 2805v.com | www.07679w.com | www.52062s.com | www.a9151e.com | www.7435t.com | yhdhz.com | www.97828n.vip | www.js00773.com | 1077eee.com | 35222tt.com | 0485y.com | www.62118b.com | www.klcpdd.com | 1168a.com | www.97171.cc | www.2945i.com | 5005c.com | www.602467.com | www.5555yh.com | 2381pp.com | 3936m.com | www.7036d.com | www.458458.net | bet89226.com | www.21202y.com | www.8148.com | w99345.am | 6146a.com | www.yh7788.bet | www.3643v.com | dzc106.com | www.738742.com | www.9149.com | 7004.am | 2649k.com | www.81233c.com | www.hg8445.com | c14666.com | www.001865.com | www.68365d.com | www.4938o.com | 5008999.com | www.801279.com | www.6880gg.com | www.lb9.com | gh7105.com | www.621441.com | www.06386611.com | www.67797e.com | 3y5511.com | www.341977.com | www.flb905.com | www.zb933.com | 88993n.com | 6js345.com | www.33598l.com | www.dzj0404.com | www.402776.com | 553322EE.com | www.66332u.com | www.706wb.com | www.v1186.com | 3178v.com | bc854.com | www.972876.com | www.38138n.com | www.00829e.com | 53262d.com | yl98.cn | www.2350x.com | www.4828998.com | www.9679z.com | 66671l.com | 50067o.com | www.c146.vip | www.065925.com | www.gd3658.com | pj677x.com | 01234r.com | www.34788u.com | www.50788d.com | www.6665ylg.com | www.wn99ww.com | 30006o.com | 38365h.com | www.309825.com | www.32365.Com | www.36008.com | www.25673f.com | j3405.com | b778877.com | www.385626.com | www.xmsy.com | www.89599j.com | www.9679h.com | 22556s.com | 0289f.com | 6040044.com | www.974088.com | www.302551.com | www.4212c.com | www.hg2098.com | 33567l.com | 01234lll.com | 3467i.com | 64111g.com | www.865510.com | www.818226.com | www.55060d.com | www.56754.com | www.28758q.com | m95510.com | 9479a3.com | yy2649.com | www.262918.com | www.3126o.com | www.55717y.com | www.55060v.com | www.987559.com | www.3657z.com | 26444z.com | 66671r.com | 1775nn.com | 3004.com | www.204500.com | www.gyfc1.com | www.dy826.com | www.1308pj.com | www.qmc0055.com | www.vns76999.com | www.7025z.com | www.66664400.com | 6220l.com | 662015.com | 7508j.com | 90307w.com | 2190s.com | 9958900.com | www.544092.com | www.81gcw.com | www.52072t.com | www.vn888678.cc | www.69567q.com | www.bwinyz39.com | www.4963zz.com | www.77759c.com | www.s0311.com | www.55155h.com | www.22799.com | www.a222.cc | 5804y.com | 7792z.com | 80368m.com | 4242hd.com | 3336.com | 8344222.com | 4136m.com | 01234n.com | 7742d.com | hh03.com | 9694p.com | 1213lll.com | 8901.com | 82365n.com | jj40033.com | 3788gg.com | 01234bb.com | 00882007.com | wns950.com | 22299dd.com | yy080.net | mm00558.com | 9101902.bet | pjjiangsu.com | 33318n.com | 0747kk.com | ambyc3.com | aipin99.me | www.hg08678.com | www.6491x.com | www.bwin299.com | www.4938c.com | www.7111f.com | www.hg2199.com | www.hg0658.com | www.12706.com | www.85857o.com | www.8967w.com | www.33112211.com | www.jsc0044.com | www.yl1981.com | www.33997t.com | www.6fa500.com | www.669772.com | www.293399.com | 8988i.com | x48v.com | 1916w.com | 3435.com | 36736.com | www.76543p.com | www.553418.com | www.hg7211h.com | www.5099gg.com | www.blr0309.com | www.868339.com | www.3416z.com | www.703027.com | www.36166a.com | 0906vip7.com | f14666.com | 55818w.com | www.my068.com | www.js778911.com | www.4212m.com | www.1764n.com | www.36788y.com | www.812870.com | 0788.com | 6611412.com | 2757a.com | 20779966.com | www.3002229.com | www.58665n.com | www.97994a.com | www.36166z.com | www.602913.com | feicai0816.com | ly88888818.com | 68228y.com | www.7435b.com | www.654777.com | www.7225e.com | www.7239a.com | www.795335.com | ww5443.com | 005873.com | 7989e.com | www.4270pp.com | www.16181y.com | www.7714s.com | www.636220.com | 6601.co | 67890hhh.com | www.pj11588.com | www.5506132.com | www.588714.com | www.66qxc.com | 48586.com | 33395757.com | www.65533d.com | www.hjdc48.com | www.3691o.com | www.922109.com | 015114.com | 20160913.vip | www.0860z.com | www.9570118.com | www.9478t.com | 22556.com | ddd67890.com | www.2090922.com | www.4963hh.com | www.60108z.com | www.26878i.com | 99909r.com | www.0333hg.com | www.b7b8.com | www.9818g.cc | 88775002.com | ems6368.com | www.sun09.com | www.k32031.com | www.897296.com | 93936t.com | feicai0576.com | www.bmw3350.com | www.588vip.net | www.90305a.com | y7062.com | www.00829t.com | www.1754o.com | www.899427.com | ylzz4447.com | www.8851i.com | www.1064e.com | www.dao70.com | 55822.com | 7744nnn.com | www.twcp05.com | www.66376.com | 1294w.cc | 001dyj.com | www.y4042.com | www.32123n.com | vbet.io | 168cp-k.com | www.1168j.com | www.c5819.com | 118888h.com | www.hg0128.com | www.80188e.com | www.533551.com | hd55331.com | www.wn99yyy.com | www.095928.com | 88905353.com | 2776t.com | www.3agcpw.com | www.czg4.com | j1458.com | www.453009.com | www.89777o.com | 1977xpj.com | 17784.com | www.77658yy.com | www.356776.com | y088.cc | www.818msc.net | www.gocp1.com | 0134.com | www.44799.com | www.6661a.cc | 5360uu.com | www.vip8xpj.com | www.36788j.com | q6q66.com | www.4938x.com | www.hg6767b.com | 7x3777.com | www.h7788g.com | www.706wb.com | 0011341.com | www.333133n.com | www.yh66766.com | p888888.com | www.f27229.com | www.7239n.com | hggjtg2.com | www.828685.com | www.9149i.com | 838388e.com | www.js12258.com | www.bei01.com | 67890o.com | www.938i.cc | www.335294.com | 2649y.com | www.4546700.com | 51335e.com | www.39695.com | www.340095.com | 2359911.com | www.948884.com | www.901175.com | 87578007.xyz | www.8080999j.com | 66458d.com | www.v3302.com | www.50054r.com | 26849.com | www.5049b.com | www.178135.com | 9411ooo.com | www.6939q.com | tz1333.com | www.pj6668.com | www.504533.com | 111122mm.com | www.2418001.com | 7989f.com | www.hyzsvip.com | www.196203.com | 5517.com | www.53900n.com | 4488vv.com | www.4996km.com | 3245x.com | www.58767.com | www.589244.com | 6830ee.com | www.bet73t.com | 4995r.com | www.680556.com | 66300vip39.com | www.786666.com | www.139733.com | www.365109f.com | www.qmfc9.com | 3844d.com | www.yh75060.com | 4465k.com | www.063801p.com | 1389t.com | www.138cpv.com | 333js.com | www.11777999.com | www.88266e.com | www.7830l.com | www.699915.com | www.pj550000.com | www.qucw5.com | 87665i.com | www.34788d.com | 8765blr.com | www.bet63.com | 2146z.com | www.75650.net | 4995z.com | www.2345678.pw | 3846uu.com | www.491407.com | 7893w10.com | www.07679a.com | 20188b.com | www.22pjjt.com | 756756d.com | www.640268.com | 92266i.com | www.4102q.com | so.cc | www.97828o.vip | 4036r.com | www.9989569.com | 40033h.com | www.8887989.com | v8381.com | www.79520c.com | 4442546.com | www.7793b.com | mgmvip688.com | www.c142.vip | www.bet365228.com | www.534680.com | www.902505.com | 1364.com | www.5966hhh.com | 88000.com | www.3066gg.com | 32555t.com | www.3303.com | zhcp52.com | www.gyfc8.com | www.bet1105.com | www.156050.com | www.msc353.com | 712930.com | www.sha4000.com | 7599r.com | www.40R.COM | 2546s.com | www.635893.com | www.3467o.com | 59759i.vip | www.00772m.com | 67890vvv.com | www.81520b.com | 118888o.com | www.368200.com | www.88msa.com | 2836518.com | www.ee00668.com | 365vip300.cc | www.w459.com | www.5319uuu.com | 0600w.cc | www.38138h.com | 4955.com | www.can80.com | www.7111u.com | 3788u.com | www.302161.com | 38850077.com | www.cai009.vip | www.qiudala.org | 2776y.com | www.hg1777.com | feicai0377.com | www.622507.com | www.888345.com | 88455.com | www.bet73s.com | www.js507713.com | 0526.com | www.01991.com | 3189b.com | www.433737.com | www.mng55.com | 2324www.com | www.1035d.com | www.bd0088.com | 55009193.com | www.99383.com | www.654bet.com | hccy365.com | www.14341434.com | www.81866e.com | 58801dh.com | www.1030360.com | 2381qqq.com | www.m.cp533pc.vip | www.71071k.com | 78332.com | www.355204.com | www.4212o.com | amjs8033111.com | www.502135.com | www.bccp877.com | df8g.com | www.235812.com | www.5250666.com | cc2205.com | www.044592.com | www.1559502.com | 2506i.com | laok000.com | www.63800v.com | www.dhy0707.com | 18438p.com | www.js20016.com | www.9702xx.com | 8547kk.com | www.22dwj.com | www.kbcp1.com | 891567.com | www.324377.com | www.06820a.com | 11422z.com | hh1331.com | www.88065b.com | www.66136cc.com | p67890.com | www.510860.com | www.00772r.com | 5651.com | yy634.com | www.73166c.com | www.8786hg.com | 87363c.com | www.047788.com | www.8577b.cc | www.vvww.6693005.com | 69445599.com | www.652188.com | www.3983151.com | www.xx0088.com | www.150196.com | www.32031g.com | 496ll.com | x86006.com | www.akw4.com | www.50052p.com | www.5028hg.com | 6487.com | 5906ll.com | www.5647.com | www.kj07.com | am8833.net | 2418x.com | www.40484.com | www.27363o.com | 452357.com | pj5556.com | www.607325.com | www.44pjjt.com | www.66gg940.com | 88w55.vip | 444000j.com | www.52303b.com | www.79500n.com | www.a8a8789.com | 22883g.com | www.330276.com | www.6768997.com | www.848033.com | 3049.com | 3679kk.com | www.450689.com | www.9996yy.com | www.9426.com | hg07111.com | 2267w.com | www.330587.com | www.pj99m.com | www.9611h.com | 889a.net | 713jc.com | 118cp4008123.com | www.7777y.cc | www.4058bb.com | www.yh8402.com | 3301866.com | 1479i.com | www.355711.com | www.c17848.com | www.2206488.com | www.3459n.com | 5309.com | 1168y.com | www.205882.com | www.68568f.com | www.00773t.com | www.903253.com | feicai0713.com | 87665m.com | 883399y.com | www.gczj2.com | www.q94600.com | www.lygxs.com | www.vns9938.com | 2266buyu.com | 9158ba.com | 2443c.com | www.c148.vip | www.679950.com | www.50999x.com | www.377088.com | 584575.com | 5856e.com | 3mgmrrr.com | www.188737.com | www.3479i.com | www.90365vip.com | www.752568.com | www.31896000.com | 3176655.com | tt6836.com | 26668h.com | 5003xx.com | www.369477.com | www.6364k.com | www.48330s.com | www.xpj2040.com | www.7777hx.com | www.ylg.net | 256777e.com | 86611n.com | 4136u.com | 293365.com | www.490780.com | www.24czj.com | www.55238d.com | www.9895z.com | www.u456x.com | www.701439.com | www.9444hg.com | 667766d.com | 73055p.com | 8159v.cc | 111hg88.com | 15g9.net | 500000572.com | www.202901.com | www.881803.com | www.8667p.com | www.55785.com | www.792087.com | www.9170011.com | www.5504e.com | www.66632.com | www.16297788.com | www.alpk888.com | js88813.com | 83377d.com | 55331l.com | 1407.com | 4231d.com | 80036.com | 4377.com | 67890gg.com | 73055i.com | 33ll8331.com | wn99yyy.com | www.67258a.com | www.338053.com | www.947547.com | www.95wcp.com | www.56579.cc | www.50052k.com | www.550021j.com | www.950185.com | www.789977.cc | www.h36555.com | www.c93919.com | www.07679j.com | www.8099777.com | www.601710.com | www.567678.cc | www.o1432.com | www.c30666.vip | www.hg8zz.com | www.848777l.com | www.16181k.com | www.5446hh.com | www.04567v.com | www.4212b.com | www.hg173j.com | www.4963ss.com | www.q32939.com | www.5207666.com | www.mgm555555.com | www.bwinyz05.com | www.781516.com | www.06820b.com | www.99552gg.com | www.86339h.com | www.9187a.com | www.975277.com | www.8zz66.com | www.mgm777c.com | www.a773776.com | www.50080m.com | www.3126j.com | www.c1396.com | www.745557.com | www.501264.com | www.106096.com | 30007a.com | 80060101.com | 66648t.com | 0234nn.com | 3405ttt.com | 00kk8331.com | 55155n.com | www.85770b.com | www.77449.com | www.6372010.com | www.5966mmm.com | www.hg8bb.com | www.pjc00.vip | www.28891h.com | www.33112d.com | www.976387.com | www.369061.com | 88304j.com | 97570yl.com | 2222k0.com | 670135.com | 5446kk.com | www.gef777.com | www.781msc.com | www.5049f.com | www.2021r.com | www.sd58qp.com | www.9818l.cc | www.599487.com | 9789886.com | 112d.net | 4288vv.com | 4595599.com | www.333222f.com | www.hg2020y.com | www.5360.com | www.hgbet3.com | www.71233w.com | www.827737.com | 691 | 99009193.com | 88903636.com | 22335156.com | www.nnn9702.com | www.c31d.vip | www.55pjjt.com | www.58870t.com | www.665772.com | kk3189.com | 8159.com | 3957l.com | www.050301.com | www.230230zf.com | www.v2552.com | www.9155d.cc | www.250757.com | 5144l.com | pj88qq.com | www.629554.com | www.js12258.com | www.6880kk.com | www.16878u.com | www.589344.com | 8290m.com | 7003uu.com | www.hg55768.com | www.xw3344.com | www.07679i.com | www.1035s.com | 77727720.com | 0683g.com | 6220n.com | www.6786j.com | www.533880.com | www.8667z.com | 44wb8.com | 11472222.com | 5004ss.com | www.336246.com | www.8899nsb.com | www.hx6666.com | 73999x.com | 3482l.com | www.www-36525h.com | www.wd716.com | www.52022.tv | www.djcp002.com | feicai0575.com | 62620029.com | www.243888.com | www.68993231.com | www.504533.com | yy4255.com | 970816.com | www.10482.com | www.0194003.com | www.215463.com | 0683h.com | www.hg4498.com | www.xy046.com | www.33997d.com | 11668331.com | y088.cc | www.yh77998.com | www.2997771.com | www.717228.com | c91234567.com | 1728862.com | www.85857a.com | www.26299h.com | 99306l.com | owlsllzv.cn | www.505444.com | www.89777k.com | www.89894u.com | 23599x.com | www.2846o.com | www.8905n.com | www.530196.com | a3685.com | www.vns68.com | www.87708.vip | www.689632.com | sbdjgj.vip | www.33388806.com | www.33678dd.com | www.605366.com | z1458.com | www.333133l.com | www.56011b.com | www.178726.com | 4152e.com | www.c6444.com | www.3978m.com | 5004qq.com | 2247c.com | www.hh58.com | www.cai001.vip | p66607.com | www.3459n.com | www.h94600.com | qq365t.com | 00773ee.com | www.3846l.com | www.cpkk6.com | zhcp26.com | www.3643d.com | www.81608f.com | 083900.com | 56988n.vip | www.848777t.com | www.558402.com | 4025s.com | www.hg7743.com | www.2988cai.com | bwinyyy.com | www.588sun.com | www.2418j.com | 9694r.com | www.a8a8234.com | www.0600v.cc | 11163366.com | 3136ll.com | www.bet91483.com | www.452823.com | 58moneys.com | www.79500c.com | www.603774.com | 365102.com | www.55526h.com | www.758500.com | 79333040.com | www.2945h.com | www.657259.com | hwapp1.com | www.83309.com | www.452805.com | 80368qq.com | www.43468.com | www.188937.com | 2543003.com | www.46630.info | 71366k.com | www.66664400.com | www.554690.com | 38244x.com | www.87680g.com | www.66376w.com | 1489m.com | www.872878.com | www.972283.com | 5666711.com | www.jz3666.com | www.89894d.com | 185647.com | www.q63568.com | 364850.com | www.4052i.com | www.78gcw.com | 7726mmm.com | www.88166r.com | www.52072a.com | 78110066.com | www.0967005.com | 30688c.com | www.82799b.com | www.622507.com | 7935t.com | www.0072721.com | pp3100.com | www.hg7788.in | www.602742.com | 2381ii.com | www.yh8899.bet | 6322.com | www.360049.com | www.318075.com | 4630077.com | www.hg355.tw | 9646l.com | www.3g88.com | 2418.com | www.js520456.com | www.960923.com | 3379zz.com | www.55238a.com |