public class GenericText{
//泛型的簡單實用,
public static void main(String[] args) {
//根據你自己的需求可以變換你需要的類型,我下面用String類型
Genericdemo<String> gd = new Genericdemo<>();
gd.setX("10");
gd.setY("20");
System.out.println(gd.getX()+"---"+gd.getY());
}
static class Genericdemo<T>{
public T x;
public T Y;
//寫一個構造方法,來接通主類
public T getX() {
return x;
}
public void setX(T x) {
this.x = x;
}
public T getY() {
return Y;
}
public void setY(T y) {
Y = y;
}
}
}
列印結果:
二、如何在構造方法中使用泛型
public class GenericText01<T>{
public static void main(String[] args) {
//根據你自己的需求可以變換你需要的類型,我下面用String類型
Genericdemo<String> gd = new Genericdemo<>("我在構造方法中使用了泛型");
System.out.println(gd.getValue());
}
//我們在公共類使用單個泛型
static class Genericdemo<T>{
public T value;
//構造方法中直接用T來代替即可
public Genericdemo(T value) {
super();
this.value = value;
}
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
}
}
列印結果:
三、如何使用多個泛型
public class GenericText01{
public static void main(String[] args) {
//根據你自己的需求可以變換你需要的類型,我下面用String類型
Genericdemo<String,Integer> gd = new Genericdemo<>("我在構造方法中使用了泛型",);
System.out.println(gd.getValue()+"----"+gd.getKey());
}
//我們在公共類使用多個泛型
static class Genericdemo<T,k>{
public T value;
public k key;
//構造方法中直接用T來代替即可
public Genericdemo(T value) {
super();
this.value = value;
}
public T getValue() {
return value;
}
public Genericdemo(T value, K key) {
super();
this.value = value;
this.key = key;
}
public K getKey() {
return key;
}
public void setKey(K key) {
this.key = key;
}
}
}
列印結果:
四、泛型中的通配符,用于接受各種類型
public class GenericText02<T>{
public static void main(String[] args) {
//根據你自己的需求可以變換你需要的類型,我下面用String類型
Genericdemo<String> gd = new Genericdemo<>();
gd.setValue("我在測試通配符");
tell(gd);
//然後我們把gd.getValue()傳遞給一個方法
}
/*
* 這裡面<?>,裡面的“?”就是通配符,不管你上面傳遞過來的什麼類型,他都可以接受
* */
public static void tell(Genericdemo<?> gd){
System.out.println("我是傳遞過來的值,可以是任何類型: "+gd);
}
//我們在公共類使用單個泛型
static class Genericdemo<T>{
public T value;
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
@Override
public String toString() {
return this.getValue().toString();
}
}
}
列印結果:
五、在接口中使用泛型
//1、我們先寫一個接口,使用泛型,定義一個方法
interface style<T>{
public void getstyle();
}
//我們寫一個接口的子類,實作我們定義的接口,裡面的類型自己去定義
class TTT implements style<String>{
public String valueString;
public String getValueString() {
return valueString;
}
public void setValueString(String valueString) {
this.valueString = valueString;
}
public TTT(String valueString) {
super();
this.valueString = valueString;
}
//沒有用到這個方法
@Override
public void getstyle() {
}
}
public class GenericText04<T>{
//我們寫一個接口的子類,實作我們定義的接口
//泛型的簡單實用,
public static void main(String[] args) {
//建立我們子類對象
TTT gd = new TTT("我在這裡用了接口泛型");
System.out.println(gd.getValueString());
}
}
列印結果:
六、泛型方法的使用
public class GenericText03<T>{
//泛型方法
public static void main(String[] args) {
//根據你自己的需求可以變換你需要的類型,我下面用String類型
Genericdemo<String> gd = new Genericdemo<>();
String s = gd.tell("A");
System.out.println("我傳的是字元"+s);
int num = gd.tell();
System.out.println("我傳的是數字"+num);
}
static class Genericdemo<T>{
public <T> T tell(T t){
return t;
}
}
}