Java基礎_集合泛型

來源:互聯網
上載者:User

標籤:處理   ide   運行時   泛型使用   資料格式   避免   Stub   元素   rac   


泛型

1.泛型概述
我們在集合中儲存資料的時候,儲存了String和Integer倆種類型的資料。而在遍曆的時候,我們把它當做是String類型處理,做了轉化,所以
會報錯。但是呢?它在編譯期間卻沒有告訴我們。所以我們覺得這一個設計的不好。所以集合也模仿著數組中在定義之前指明資料的類型,在
建立對象的時候明確元素的資料類型。這樣就不會存在問題了。這一種技術被稱為泛型。

2.泛型
是一種把類型明確的工作延遲到建立對象或者調用方法的時候才去明確的特殊類型。參數化型別,把類型當作參數傳遞.

3.泛型定義資料格式
<資料類型>
注意:這裡面的資料類型只能夠是參考型別。

4.泛型的好處
(1).把運行時期的問題提前到了編譯期間
(2).避免了強制類型轉換
(3).最佳化了程式設計,解決了異常提示


5.泛型使用舉例:
public class Test {
public static void main(String[] args) {
List<String> lt=new ArrayList<String>();
lt.add("hello");
lt.add("world");
lt.add("java");
for(int i=0;i<lt.size();i++){
System.out.println(lt.get(i));
}

}
}
//輸出結果
hello
world
java


6.泛型類的使用
(1).概述:
把泛型定義在類上。
public class ObjectTool<T>()//這裡面的T表示的就是變了的類型,T-->type

(2).代碼實現
學生類:
public class Student implements Cloneable{
//學生名字
private String name;
//年齡
private int age;

//無參
public Student(){

}

//帶參構造
public Student(int age, String name) {
super();
this.age=age;
this.name=name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}
}
//測試類別
public class Test {
public static void main(String[] args) {
List<Student> lt=new ArrayList<Student>();
Student st=new Student(10,"張三");
Student st1=new Student(11,"李四");
Student st2=new Student(12,"王五");
lt.add(st);
lt.add(st1);
lt.add(st2);
for(int i=0;i<lt.size();i++){
System.out.println(lt.get(i).getAge()+"----"+lt.get(i).getName());
}
}
}
//輸出結果:
10----張三
11----李四
12----王五


7.泛型介面
(1).所謂的泛型介面就是把泛型定義在介面上。

(2).代碼實現
//定義介面
public interface Testfx<T> {
public abstract void show(T t);
}
//定義介面實作類別
public class TestfxImpliment<T> implements Testfx<T> {
@Override
public void show(T t) {
// TODO Auto-generated method stub
System.out.println(t);
}
}
//測試類別
public class Test {
public static void main(String[] args) {
Testfx<String> tf=new TestfxImpliment<String>();
tf.show("hello");

Testfx<Integer> tf1=new TestfxImpliment<Integer>();
tf1.show(100);
}
}

8.泛型的進階使用(萬用字元)
(1).萬用字元使用
?:任意類型,如果沒有明確,那麼就是Object以及任意的java類了。
?:extends E:向下限定,E以及其子類
?:super E:向上限定,E以及其父類

(2).代碼實現
//動物類
public class Animal {
}
//狗類
public class Dog extends Animal{
}
//貓類
public class Cat extends Animal{
}
//測試類別
public class Test {
public static void main(String[] args) {
//如果沒有明確需要的是什麼類型
Collection<Object> c1=new ArrayList<Object>();
Collection<?> c2=new ArrayList<Object>();
Collection<?> c3=new ArrayList<Animal>();
Collection<?> c4=new ArrayList<Dog>();
Collection<?> c5=new ArrayList<Cat>();
//? extends E:向下限定,E以及其子類
Collection<? extends Animal> c6=new ArrayList<Animal>();
Collection<? extends Animal> c7=new ArrayList<Dog>();
Collection<? extends Animal> c8=new ArrayList<Cat>();
//? super E:向上限定 ,E以及父類
Collection<? super Animal> c9=new ArrayList<Animal>();
Collection<? super Animal> c10=new ArrayList<Object>();
}
}

 

Java基礎_集合泛型

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.