菜鳥的java泛型學習教程

菜鳥的java泛型學習教程

說明

本文記錄java泛型的簡單使用,本文會持續更新,不斷地擴充

本文僅爲記錄學習軌跡,如有侵權,聯繫刪除

泛型類

例子1

//最簡單的泛型類
class generic01 <T>{
    private String key;
    private T value;

    public generic01() {
    }

    public generic01(String key, T value) {
        this.key = key;
        this.value = value;
    }

    @Override
    public String toString() {
        return "generic01{" +
                "key='" + key + '\'' +
                ", value=" + value +
                '}';
    }


    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }
}

使用

    public static void test01(){
        generic01<String> g1= new generic01<String>();//最簡單的泛型類
        g1.setKey("String類型的value");
        g1.setValue("hello world");
        System.out.println(g1);

        generic01<Integer> g2= new generic01<Integer>();//最簡單的泛型類
        g2.setKey("Integer類型的value");
        g2.setValue(123);
        System.out.println(g2);
    }



//結果
//generic01{key='String類型的value', value=hello world}
//generic01{key='Integer類型的value', value=123}

例子2
實體類user

package com.zsc.po;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

@Data
@NoArgsConstructor
@AllArgsConstructor
//普通用戶
public class User {
    private Integer id;
    private String name;
    private Date birthday;
    private Boolean type = false;

    public User(Integer id, String name, Date birthday) {
        this.id = id;
        this.name = name;
        this.birthday = birthday;
    }
}

實體類root

package com.zsc.po;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
//管理員
public class Root {
    private Integer id;
    private String name;
    private Date birthday;
    private Boolean type = true;
    private List<User> users;//被管理員管理的用戶普通

    public Root(Integer id, String name, Date birthday) {
        this.id = id;
        this.name = name;
        this.birthday = birthday;
    }
}

泛型類

//最簡單的泛型類
class generic02 <T>{
    private T value;

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }

    public void getInfo(){
        System.out.println("基本信息:"+value);
    }

}

使用

    public static void test02(){
        User user = new User(1,"Tony",new Date());
        Root root = new Root(1,"管理員",new Date());

        generic02 <User> u = new generic02<User>();
        u.setValue(user);
        u.getInfo();


        generic02 <Root> r = new generic02<Root>();
        r.setValue(root);
        r.getInfo();
    }

//結果
//基本信息:User(id=1, name=Tony, birthday=Fri May 29 16:12:23 CST 2020, type=false)
//基本信息:Root(id=1, name=管理員, birthday=Fri May 29 16:12:23 CST 2020, type=true, users=null)

泛型接口

//泛型接口MyInterface
interface MyInterface <T>{
    public void show(T value);
}

//泛型MyInterface實現類1
class MyInterfaceImpl <G> implements MyInterface <G>{

    @Override
    public void show(G value) {
        System.out.println("value:"+value);
    }
}

//泛型MyInterface實現類2
class MyInterfaceImpl2 implements MyInterface<String>{

    @Override
    public void show(String value) {
        System.out.println("value:"+value);
    }
}

運行

    public static void main(String[] args) {
        MyInterface myIn = new MyInterfaceImpl2();
        myIn.show("hello world");

        MyInterface<Integer> myIn2 = new MyInterfaceImpl<Integer>();
        myIn2.show(12138);

    }
    
    //結果
    //value:hello world
    //value:12138

泛型方法

public class GenericMethod {
    //泛型方法
    public static <T> void print(T t) {
        System.out.println(t);
    }


    public static void main(String[] args) {
        String str = "Hello World";
        Integer integer = 123;
        boolean b = true;
        Date bir = new Date();

        //傳入什麼類型的數據就輸出什麼類型的數據
        print(str);
        print(integer);
        print(b);
        print(bir);
    }

}


//結果
//Hello World
//123
//true
//Fri May 29 16:14:58 CST 2020
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章