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.45598u.com | 1466s.com | www.gt885.com | www.0601t.com | www.805272.com | www.112111.com | yf2111.com | 2776e.com | www.60007x.com | www.cntwsm.com | e2554.com | 50020088.com | www.lqc5.com | www.t69096.com | www.44yh765.com | 3405c.com | www.203552.com | www.789977.cc | www.358nn.com | 3559uu.com | wzdongbao.com | www.ybao9.com | www.848777i.com | www.76060m.com | 99909q.com | www.259989.com | www.5958126.com | www.xg189.com | 20167744.com | 0057a.com | www.848166.com | www.506201.com | www.948900.com | 229649.com | 3936u.com | www.339967.com | www.hg594.com | www.zzz888.com | 2776n.com | ff555n.com | www.703153.com | www.087w.com | www.7302004.com | g1429.com | 66287c.com | 2846g.com | www.lu126.com | www.0622iii.com | www.sss998.com | 9895p.com | 8535.com | 2021h.com | www.3552q.com | www.074546.com | www.77605u.com | 1077dd.com | so1111.cc | www.109026.com | www.67258b.com | www.009329.com | www.amdc55.com | 5295uu.com | 6723888.com | rr5144.com | www.449879.com | www.83993n.com | www.hg98.la | www.www-36222.com | 65005o.com | 37688k.com | 56033.net | www.712887.com | www.0270p.com | www.http://448378.com/ | www.js7669.com | js29391.com | 4025d.com | 32x1.com | www.80065e.com | www.3416g.com | www.7249f.com | www.8473e.com | www.f8118.com | www.81866m.com | aa4118.com | 2064hb.com | 3016www.com | www.620316.com | www.26163n.com | www.3691a.com | www.biying990vip.com | www.hg415.com | www.88837f.com | www.00048js.com | bopaihang.com | 5556944.com | 996622hh.com | 1429m.com | www.376519.com | www.2350g.com | www.16065y.com | www.820016.com | www.58665y.com | www.hg7684.com | www.995r.net | www.8694t.com | 2883dl.com | 5589.com | 496cc.com | 4025y.com | 363369.com | 62220.com | 32424f.com | www.596081.com | www.3416w.com | www.yh76g.com | www.jl58qp.com | www.115527p.com | www.50999j.com | www.bwin499.com | www.w444999.com | www.1347-03.com | www.glc11.com | f35151.com | a08199.com | 75959s.com | 3568yy.com | 37770778.com | 3726x.com | 9694p.com | 5099kk.com | blr3033.com | 0343i.com | 28824o.com | 2373e.com | 1077qq.com | 817458.com | cidubbs.com | www.235693.com | www.567231.com | www.809573.com | www.152012.com | www.061wy.com | www.36166f.com | www.84499f.com | www.05088.com | www.0270t.com | www.77210d.com | www.340095.com | www.37377f.com | www.7240h.com | www.120668.com | www.hg81999.com | www.1239999.cc | www.v63568.com | www.888999qipai.com | www.js89t.vip | www.1033.app | www.78949l.com | www.66332p.com | www.55885c.com | www.52511.com | www.7782f.com | www.al5888.com | www.864955.com | www.686783.com | www.599649.com | www.181599.com | www.80767o.com | www.99638i.com | www.61233i.com | feicai0511.com | 3245777.com | 58222x.com | bwin2808.com | 8538x.com | 02888722.com | 97297s.com | www.bb1365.com | www.8882558.com | www.16297777.com | www.le888w.com | www.58665w.com | www.77658xx.com | www.792082.com | www.9646f.com | www.6832w.com | www.606643.com | www.39500o.com | js14r.com | 4289y.com | 78808a.com | jindaye88.com | www.68666i.com | www.88837q.com | www.rb568.com | www.cn365s.com | www.js56772.com | www.12455i.com | www.ban54.com | www.403770.com | 228888c.com | 7893w48.com | 40033f.com | 55818z.com | www.hg6969.net | www.135605.com | www.20199gg.com | www.491977.com | www.606756.com | www.37437.cc | www.400322.com | 3614h.com | 115959.com | z2490.com | www.am9977.com | www.65866.com | www.19019v.com | www.9822az.com | www.00840r.com | www.401345.com | xg8111.com | 69440055.com | lhc958999.com | www.26yh76.com | www.68199.com | www.win9789.com | www.kc19.com | 7792g.com | p888ab.com | 2649ww.com | www.35441777.com | www.4136k.com | www.35898b.com | www.799849.com | 99885002.com | 86611a.com | www.8819819.com | www.drf0561.com | www.3w222.com | www.9478w.com | www.029079.com | 3225m.com | 2247kk.com | www.xpj4464.com | www.673888y.com | www.21202m.com | www.082707.com | feicai0879.com | 88770.com | www.456228.com | www.44118.net | www.974088.com | 9103pp.com | 40033iii.com | www.shenbo558.com | www.77731s.com | www.sx139975.com | www.210883.com | 22cc8332.com | www.147755.com | www. | www.9646k.com | www.rcw321.com | cai89.cc | www.7435r.com | www.4212p.com | www.33358c.com | bj0111.com | 4136g.com | www.vns00022.com | www.88325j.com | www.976115.com | sbf533.com | 123456vip.cc | www.009959.com | www.2418d.com | www.296180.com | 861224.com | 3016ddd.com | www.27363o.com | www.60007h.com | 463e7.com | 23599w.com | www.a4737.com | www.567678.cc | www.c6121.com | 2709h.com | fzcrd.com | www.821623.com | www.6364t.com | yabo9000.com | 56987dd.com | www.5504v.com | www.58699.cc | 73055.vip | 3258q.com | www.27363w.com | www.5854l.cc | 10050714.com | www.20088hg.com | www.38138m.com | www.853548.com | 30150011.com | www.21365jj.com | www.44118s.com | www.493552.com | 93922x.com | www.7111z.com | www.68365x.com | www.43131k.com | 2127jj.com | www.828501.com | www.7782e.com | 3mgmiii.com | www.jj881.com | www.63606t.com | www.303053.com | 53262ii.com | www.boma0194.com | www.122662.com | 89777s.com | dd2205.com | www.1168o.com | www.959553.com | 0209.com | www.wnsr7788.cc | www.66621j.com | 2257016.com | www.hg8728.net | www.5856866.com | 56033.net | 18438a.com | www.663558.com | dzc04.com | 2247x.com | www.0082737.com | www.278688.com | pj911a.cc | www.4972v.com | www.525059.com | 62222d.com | www.68277.com | www.763226.com | dd01234.com | www.xj666g.com | www.918109.com | 4465i.com | www.3846m.com | www.589344.com | 56787hh.com | www.98698c.com | www.390685.com | 883399u.com | www.955244.com | 11018h.com | www.4759bb.com | www.win1238.com | 2287c.com | www.cai73dd.com | www.21202f.com | 23815.com | www.722168.com | www.814646.com | tz2200.com | www.38138o.com | jnbzsj.com | www.223456p.com | www.81678q.com | js26311.com | www.774567.com | www.739366.com | qq365y.com | www.44118h.com | 6245w.com | www.nbe666.com | www.9478z.com | 4717.com | www.4058u.com | feicai0933.com | www.98599.com | www.74972.com | 32666f.com | www.8967s.com | ylzz1112.com | www.18599.com | www.9155e.com | qqq7570.com | www.1559505.com | 829330.com | www.7196x.com | www.818827.com | dd2205.com | www.798588.net | 2127oo.com | www.5008637.com | n888go.com | www.ra888vip.com | www.hqcp2.com | aobo26.com | www.55755.com | 131tt.net | www.t9478.com | 1389.com | www.17977.com | www.581860.com | 897kk.cc | www.63500.cc | 4323t.com | www.68993271.com | xx67890.com | www.35700aa.com | 4694f.com | www.63328.cc | 9539l.com | www.173msc.com | www.271901.com | www.hg755.com | www.ch8933.com | 6868nn.cc | www.60108l.com | q888.tv | www.4833044.com | 4488aaa.com | www.40288v.com | ddh5154.com | www.cn365a.com | jdb58.com | www.cai93.com | 66458f.com | www.4699g.com | fff3405.com | www.6666zx.com | 99111hh.com | www.86939.com | P35dd.com | www.d88md01.com | 667766g.com | www.055838.com | 8988rr.com | www.23470.com | fstppx.com | www.hg206.com | 2997705.com | www.lj558.com | 3245l.com | www.5049w.com | 7792p.com | www.8967z.com | 2292958.com | www.57800p.com | 364870.com | www.89155444.com | 550111.com | www.065835.com | 2146g.com | www.26163h.com | vnsr55888.com | www.946689.com | www.am0676.com | www.196103.com | www.331suncity.net | 3066qqq.com | www.67msxfpt3.com | 61789p.com | www.4196c.com | 8159f.cc | www.bet365cn.net | 11989i.com | www.c5906.com | www.7669k.net | www.290165.com | www.274166.com | 8988z.com | www.8313o.com | mf708.com | www.87087.com | 00442007.com | www.c1663.com | www.8694y.com | 054892.com | www.55526f.com | 123456yy.cc | www.7zz66.com | feicai0559.com | www.cp8018.cc | www.88807f.com | 3678q.com | www.58665q.com | vic6308.com | www.68203g.com | 6868ii.cc | www.542672.com | www.33225.com | i444000.com | www.22pjjt.com | 8988b.com | www.c139.vip | www.j7191.com | 55818.com | www.509236.com | pj677f.com | www.744646.com | www.56748.com | 45637i.com | www.d98478.com | www.mg4355888.com | www.109076.com | www.752568.com | 55967t.com | www.1035u.com | www.v15502.com | 35252d.com | www.97828f.vip | 2247s.com | www.097710.com | www.rycp167.com | 51133dd.com | www.c229.vip | www.444559.com | 3844gg.com | www.76520d.com | www.108083.com | 8159c.cc | www.333682.com | www.ag803.com | i99345.am | www.087f.com | www.yh8006.com | 3122h.com | www.80868e.com | 11422z.com | 73055o.com | www.e2894.com | 1483ll.com | wns2.net | www.742488.com | wpmtp.com | 188.cc | www.flb904.com | www.7435p.com | 99111oo.com | www.93gj08.com | www.sb9909.com | 3304o.com | www.76520y.com | www.954555.com | 1919394.com | www.ptk3.com | www.jgj230230.com | 001122335.com | www.451117.com | www.3bet005.com | zhcp84.com | feicai0890.com | www.i98478.com | www.76543i.com | 999hg.com | www.5086w.com | www.77759c.com | 3522w.vip | www.117015.com | www.4521v.com | www.hg9953.com | qycp356.com | www.39500s.com | www.678987.com | u2490.com | www.717699.com | www.w2894.com | www.753055.com | 69990b.com | www.zfcp8.com | www.5504n.com | 30007m.com | rbbet55.com | www.89338c.com | www.4646123.com | 8569811q.com | 52599j.com | www.36584444.com | www.xy9999.com | bet99339.com | 45637v.com | www.3334331.com | www.yh1386.com | 51133l.com | 22291155.com | www.ya2019x.com | www.9005b.com | 8406.com | jdwxs.com | www.66073.cc | www.169009.com | 937012.com | 2757y.com | www.612110.com | www.r678.com | www.58777g.com | 5888.ch | 71366k.com | www.7714q.com | www.9999xx.com | 99151a.com | 2851v.com | www.956458.com | www.3577.ag | www.7830c.com | 40033uuu.com | 30006k.com | www.60007r.com | www.1168z.com | www.4759ww.com | 53206666.com | www.80767v.com | www.7714r.com | www.55526q.com | www.56733x.com | 28839x.com | www.135125.com | www.ya2019d.com | www.suncity112.com | www.033033r.com | 12274444.com | www.50054u.com | www.88266y.com | www.ggz.99233z.com | www.8124y.com | 3957o.com | 00787.com | www.slyl1.com | www.w2894.com | www.hg0782.com | 26444j.com | 2595o.com | www.50026i.com | www.26299p.com | www.55060l.com | www.b7276.com | 425511.com | zhcp83.com | www.177348.com | www.66652h.com | www.mgm868008.com | www.902505.com | 2978.com | 69446600.com | 0693i.com | www.ch8933.com | www.4809d.com | www.f4042.com | www.bwin2288.net | 56787hh.com | 4136u.com | xianboba.co | www.08xyc.com | www.1118yl.com | www.v56789.com | www.22288806.com | 28288v.com | 00774jj.com | 08159b.com | www.978231.com | www.bj1111.com | www.1100xpj.net | www.72265i.com | www.hg44768.com | pj300.cc | 1434o.com | 2844g.com | www.rcw8899.com | www.50732q.com | www.32031m.com | www.5966xxx.com | www.88837i.com | 8898111.com | 82365e.com | hggjtg8.com | 667766x.com | www.788572.com | www.80075y.com | www.bb00668.com | www.rycp167.com | www.888zr61.com | www.pj733.cc | 66p88.cc | ddd3405.com | 4052v.com | 659pj.com | www.299695.com | www.fcff0.com | www.99677m.com | www.12136l.com | www.4963hh.com | www.89266.com | www.4972dd.com | www.9844js.com | jsh500.com | rrr1915.com | 0343q.com | ylgr666.com | 9339999j.com | www.dwj0066.com | www.c1636.com | www.54400z.com | www.393379.com | www.45598e.com | www.423550.com | www.777348.com | www.88837e.com | www.85770m.com | feicai0416.com | 22ss8331.com | 159666x.com | 4165d.com | 66009193.com | 59599p.com | 566671144.com | 38345s.com | 105boo.com | www.107501.com | www.631012.com | www.806292.com | www.ybao6.com | www.26299x.com | www.hg7774.com | www.bd2019b.com | www.1779.com? | www.958661.com | www.38138u.com | www.16181c.com | www.9cai.com | www.tc8804.com | www.i7793.com | www.7302004.com88 | www.70704.net | www.8577l.cc | www.9679q.com | www.xpj8874.com | www.330099u.com | www.88807q.com | www.00829d.com | www.365815j.com | www.hjdc2001.vip | www.vns0708.com | www.213535.com | www.hg9945.com | www.4111.cc | www.4107e.com | www.r22365.com | www.3643d.com | www.9996ff.com | www.tyc357.com | www.sbet818.net | www.pj8790.com | www.88jt555.com | www.bali666.com | www.4249.com | www.14900a.com | www.hg7660.com | www.lu8810.com | www.35252o.com | www.0662777.com | www.57800q.com | www.i1432.com | www.33678ff.com | www.68365s.com | www.393379.com | www.ya2019s.com | www.77114x.com | www.5441f.com | www.hf1680.com | www.679610.com | www.315063.com | 78118866.com | 8522mmmm.com | 5608.com | 00112007.com | 77606e.com | 8547oo.com | t47479.com | www.0686333.com | www.66654666.com | www.amxj8855.com | www.383188.com | www.615053.com | www.45598m.com | www.11599111.com | www.80075l.com | www.1368x.cc | www.684077.com | www.165595.com | hg77704.com | 91019b.net | p21148.com | 642221.com | 33313s.com | www.00829e.com | www.554msc.com | www.eb000.com | www.1117795.com | www.bj699.com | www.2221102.com | www.918io.com | www.555958.com | www.07088.com | www.009ac.com | www.294218.com | www.00773q.com | www.441918.com | www.33112s.com | www.760557.com | 8203ccc.com | 73657k.com | 444167.com | 3379ss.com | www.hg5588.net | www.234224.com | www.16181w.com | www.428.com | www.1368.cc | www.256238.com | 2824.com | 77000.com | 518cp9.com | www.bet9339.com | www.660tk.com | www.1429g1.com | www.ya2019t.com | www.816830.com | 22117i.com | 1168c.com | 28485000.com | www.7830l.com | www.02.bet | www.42070013.com | www.7782c.com | www.191380.com | 9856pp.com | 159666y.com | www.kj638.com | www.660tk.com | www.60886.com | www.5854l.cc | www.43131n.com | man993.com | 8547ss.com | www.l30226.com | www.629820.com | www.1466l.com | www.870013.com | 866489.com | 8590.vip | www.pj550099.com | www.777758.com | www.r999999.com | www.bei08.com | c7454.com | 86226n.com | www.dfs788.com | www.563012.com | www.198a56.com | www.901031.com | 4880o.com | 8522oooo.com | www.67797l.com | www.99113k.com | www.77210c.com | www.178397.com | 2709m.com | d55.com | www.suncity3.com | www.23456789.pw | www.721560.com | 1985555.com | 234904.com | www.cs55222.com | www.w84s.com | www.938537.com | xpj677u.com | bwin8t.com | www.292030.com | www.75600w.com | www.619989.com | 20779999.com | 9149a.com | www.j0055.com | www.954321u.com | www.090040.com | 2021t.com | www.hg2877.org | www.71071b.com | www.5086o.com | 32126o.net | nn500k.com | www.zr6668.com | www.7240l.com | www.396990.com | 0652g.com | www.0636c.com | www.4136c.com | www.27czj.com | 8547r.com | 0600r.cc | www.18765.com | www.55885m.com | 588zzz.cc | 2146c.com | www.dg533.com | www.99080044.com | 0139365.com | 26668g.com | www.0wdgj.com | www.1429.net | o72227.com | bet28f.com | www.797077.com | www.bet73a.com | jsjlb77.com | 55818z.com | www.813365.com | www.5484f.com | 00048o.com | 5429d.com | www.c30666.vip | www.866309.com | 3522u.vip | www.3459b.com | www.780780b.com | www.77210a.com | 55331pp.com | www.hg7651.com | www.61233m.com | 59859.com | 6556m.com | www.4996dq.com | www.629813.com | 4036ww.com | www.51456.com | www.770802.com | 66339193.com | 8827hh.com | www.673888k.com | www.591023.com | 00212.com | www.vns3591.com | www.60007b.com | 1382.cm | www.jqb5.com | www.40288p.com | www.50054e.com | 85698y.com | www.448246.com | www.901387.com | 01234ff.com | www.hg3098.com | www.18593.com | j2857.com | www.66930011.com | www.460433.com | 69440066.com | www.h7714.com | www.48330l.com | ca1055.com | 9420s.com | www.42070013.com | 70118c.com | js14j.com | www.xpj3133.cc | 777p1.cc | www.pj550000.com | www.12136b.com | v3144.com | www.35530.com | www.xpj33987.com | 8988c.com | www.55123.com | www.8998786.com | 86688001.com | www.hg1137.com | www.0601q.com | 30174400.com | www.58777q.com | www.61233b.com | 2222k5.com | www.4797.com | www.933777a.com | 4541b.com | www.1168o.com | www.271901.com | hg1568.com | www.99552hh.com | 81511n.com | www.365uu6.com | www.99677x.com | 33uu8331.com | www.987111.com | www.983003.com | 8547pp.com | www.777jyh.com | x62365.com | www.6686tk.cc | www.3978u.com | f465.com | www.js436.com | www.685053.com | tzvip2024.com | www.w84.com | yy634.com | www.00037b.com | www.3207.com | 2142244.com | www.57800m.com | gcw82.com | www.388365.net | www.36166w.com | 23599x.com | www.50999w.com | yf5599.com | www.vip7897.com | www.78995.com | 36401177.com | www.0042737.com | 22296nn.com | www.dy6611.com | www.08992.com | y73888.com | www.4058mm.com | 99151z.com | www.zr918.com | www.918109.com | 9995j.cc | www.68365m.com | bjl1588.com | www.hg0699.com | www.210155.com | www.8058.la | www.68568q.com | 1104006.com | www.4996gl.com | 3121tt.com | www.18777s.com | www.574277.com | 5369h.com | www.83033c.com | 54146688.com | www.06387799.com | 22749.com | www.57289.com | xl18.org | www.hg0110.com | www.00ttz.com | 44cc8331.com | www.004549.com | 118cp.com | www.x80288.com | 80368i.com | www.613177.com | 26668i.com | www.txck.cc | www.619616.com | www.033033w.com | www.5522l.cc | 88851e.com | www.82894d.com | hjcp114.com | www.y8917.com | 5647y.com | www.55545d.com | 12742v.com | www.777794.com | 88048.com | www.095985.com | www.83033v.com | www.29886s.com | www.544093.com | www.7669c.net | www.666405.com | www.tqdc444.com | www.938901.com |