<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 泛型 [TOC] ## 導學 在之前學過的集合框架中,List和Map都使用了泛型技術來確認其內容的數據類型。 如果不使用泛型,在程序運行階段,會帶來數據類型轉型的錯誤風險。 ~~~ List<String> list = new ArrayList<String>(); list.add("tom"); for (int i = 0; i < list.size(); i++) { String obj = list.get(i); System.out.println(obj); } List list2 = new ArrayList(); list2.add("helen"); list2.add(2); // 自動裝箱成Integer list2.add(true); // 自動裝箱成Boolean for (int i = 0; i < list2.size(); i++) { String obj = (String)list2.get(i); // 此處是有風險的 } ~~~ 在Java中,使用變量之前,必須要先定義變量的數據類型,存在一種特殊的現象,就是多態(數據類型是父類,實現對象是子類),變量賦值不一定要完全和數據類型一致,可以賦予子類對象給它。 ~~~ public class Client2 { public static void main(String[] args) { Point point = new Point(); point.x = "東經102°"; point.y = "北緯32°"; point.x = 102; point.y = 32; String s = (String) point.x; point.print(); } } class Point { Object x; Object y; public void print() { System.out.println(x + "" + y); } } ~~~ >[danger]向下轉型會帶來數據風險的(ClassCastException) 不使用泛型的缺點:需要強制轉換;可向集合中添加任意類型的對象,存在風險 泛型的使用:在接口/類名后的尖括號內加上類型,如 `List list=new ArrayList(); `同時,變量聲明時的泛型類和其實現類時的泛型類必須相同,即“=”前后的尖括號中的類型需要一致 ~~~ class Animal{} class Cat extends Animal {} List<Animal> list = new ArrayList<Cat>(); 這樣是不允許的。變量聲明的類型必須匹配傳遞給實際對象的類型 ~~~ ## 泛型作為方法參數 **泛型作為方法參數** >[info] 案例: > 定義一個抽象類Goods,包含抽象方法sell(); > 分別定義各類商品Book,Clothes和Shoes繼承Goods,并實現sell()方法,輸出一句話,如sell books > 定義一個商品銷售類GoodsSeller,模擬銷售,包括方法:public void sellGoods(List goods),循環調用list對象的sell() 方法 ~~~ public abstract class Goods { public abstract void sells(); } public class Books extends Goods{ @Override public void sells() { System.out.println("sell books"); } } public class Clothes extends Goods { @Override public void sells() { System.out.println("sell clothes"); } } public class Shoes extends Goods { @Override public void sells() { System.out.println("sell shoes"); } } public class GoodsSeller { //代表只要goods參數保存的元素是Goods或者Goods的子類,都可以 public void sellGoods(List<? extends Goods> goods) { //調用集合中的sell方法 for(int i = 0; i < goods.size(); i++) { Goods g = goods.get(i); g.sells(); } } } public class Test { public static void main(String[] args) { //定義books相關的list List<Books> bookLst = new ArrayList<Books>(); bookLst.add(new Books()); bookLst.add(new Books()); //定義Shoes相關的list List<Shoes> shoesLst = new ArrayList<Shoes>(); shoesLst.add(new Shoes()); shoesLst.add(new Shoes()); //定義Clothes相關的list List<Clothes> clothesLst = new ArrayList<Clothes>(); clothesLst.add(new Clothes()); clothesLst.add(new Clothes()); GoodsSeller gdSeller = new GoodsSeller(); gdSeller.sellGoods(bookLst); } } ~~~ ## 自定義泛型類 ~~~ public class Demo { public static void main(String[] args) { Point<Integer, Integer> p = new Point<Integer, Integer>(); p3.x = 1; p3.x = 2; } } public class Point<T1, T2> { T1 x; T2 y; } ~~~ >[warning] 類的泛型在className之后定義,只有定義的泛型類型才能在類中使用。 即我們定義只有先定義類名后的泛型標志符的時候,才能使該泛型類使用泛型定義方法和成員變量。 ## 自定義泛型方法 >[success] 泛型方法不一定要寫在泛型類中 ~~~ /** * 定義泛型方法 **/ public class GenericMethod { //定義泛型方法的時候需要將泛型放在訪問修飾符和返回值中間 public <T extends Number> void print(T t) { System.out.println(t.toString()); } public static void main(String[] args) { GenericMethod gm = new GenericMethod(); //可以通過泛型方法實現一個方法傳遞不同的參數 //gm.print("123");規定泛型的繼承限定之后,該行語句就是錯誤的了 gm.print(123); gm.print(123f); } } ~~~ >[warning] 泛型一般使用于框架設計,在實際的應用開發中較少運行 ## Java泛型中的標記符含義: E - Element (在集合中使用,因為集合中存放的是元素) T - Type(Java 類) K - Key(鍵) V - Value(值) N - Number(數值類型) ? - 表示不確定的java類型 `<K,V>`就是代表鍵值對的泛型 ## 練習 一、選擇 1. 以下語句正確的是:(多選) ~~~ A. List<String> list=new ArrayList<String>(); B. List<Integer> list=new ArrayList<>(); C. 已知Cat是Animal類的子類,則List<Animal> list=new ArrayList<Cat>(); D. List<Object> list=new ArrayList<Integer>(); ~~~ 二、編程 1. 通過練習,掌握泛型作為方法參數的應用,具體內容見任務要求。運行效果如下: ![](https://img.kancloud.cn/ce/c7/cec73c122fb258e90e660de13b0e4852_189x118.png) 任務: ![](https://img.kancloud.cn/cc/4a/cc4a222216f298eaaf2c578c09d96186_337x360.png)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看