Java Generic <T> T and T usage

<T> TRepresents the return value is a generic, what is transmitted, it returns what type of data, and the individual Tis showing you pass type limiting parameters, in this case, returned through a generic way, obtaining each of the first set data, return value <T> T and Timplement two methods

<T> T usage

This <T> T represents the return value is a generic T, T is a placeholder used to tell the compiler that would let me keep this thing, so I compile time, to tell you.

package com.yellowcong.test;

import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.formula.functions.T;

public class Demo { public static void main(String[] args) { Demo demo = new Demo(); //获取string类型 List<String> array = new ArrayList<String>(); array.add("test"); array.add("doub"); String str = demo.getListFisrt(array); System.out.println(str); //获取nums类型 List<Integer> nums = new ArrayList<Integer>(); nums.add(12); nums.add(13); Integer num = demo.getListFisrt(nums); System.out.println(num); } /** * 这个<T> T 可以传入任何类型的List * 参数T * 第一个 表示是泛型 * 第二个 表示返回的是T类型的数据 * 第三个 限制参数类型为T * @param data * @return */ private <T> T getListFisrt(List<T> data) { if (data == null || data.size() == 0) { return null; } return data.get(0); } } 

T Usage

The return value, write directly Trestriction type indicating parameters, this method is generally used for operating a common class object, and then obtain what is inside the set information.

package com.yellowcong.test;

import java.util.ArrayList;
import java.util.List; public class Demo2<T> { public static void main(String[] args) { //限制T 为String 类型 Demo2<String> demo = new Demo2<String>(); //获取string类型 List<String> array = new ArrayList<String>(); array.add("test"); array.add("doub"); String str = demo.getListFisrt(array); System.out.println(str); //获取Integer类型 T 为Integer类型 Demo2<Integer> demo2 = new Demo2<Integer>(); List<Integer> nums = new ArrayList<Integer>(); nums.add(12); nums.add(13); Integer num = demo2.getListFisrt(nums); System.out.println(num); } /** * 这个只能传递T类型的数据 * 返回值 就是Demo<T> 实例化传递的对象类型 * @param data * @return */ private T getListFisrt(List<T> data) { if (data == null || data.size() == 0) { return null; } return data.get(0); } }

<T> TRepresents the return value is a generic, what is transmitted, it returns what type of data, and the individual Tis showing you pass type limiting parameters, in this case, returned through a generic way, obtaining each of the first set data, return value <T> T and Timplement two methods

<T> T usage

This <T> T represents the return value is a generic T, T is a placeholder used to tell the compiler that would let me keep this thing, so I compile time, to tell you.

package com.yellowcong.test;

import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.formula.functions.T;

public class Demo { public static void main(String[] args) { Demo demo = new Demo(); //获取string类型 List<String> array = new ArrayList<String>(); array.add("test"); array.add("doub"); String str = demo.getListFisrt(array); System.out.println(str); //获取nums类型 List<Integer> nums = new ArrayList<Integer>(); nums.add(12); nums.add(13); Integer num = demo.getListFisrt(nums); System.out.println(num); } /** * 这个<T> T 可以传入任何类型的List * 参数T * 第一个 表示是泛型 * 第二个 表示返回的是T类型的数据 * 第三个 限制参数类型为T * @param data * @return */ private <T> T getListFisrt(List<T> data) { if (data == null || data.size() == 0) { return null; } return data.get(0); } } 

T Usage

The return value, write directly Trestriction type indicating parameters, this method is generally used for operating a common class object, and then obtain what is inside the set information.

package com.yellowcong.test;

import java.util.ArrayList;
import java.util.List; public class Demo2<T> { public static void main(String[] args) { //限制T 为String 类型 Demo2<String> demo = new Demo2<String>(); //获取string类型 List<String> array = new ArrayList<String>(); array.add("test"); array.add("doub"); String str = demo.getListFisrt(array); System.out.println(str); //获取Integer类型 T 为Integer类型 Demo2<Integer> demo2 = new Demo2<Integer>(); List<Integer> nums = new ArrayList<Integer>(); nums.add(12); nums.add(13); Integer num = demo2.getListFisrt(nums); System.out.println(num); } /** * 这个只能传递T类型的数据 * 返回值 就是Demo<T> 实例化传递的对象类型 * @param data * @return */ private T getListFisrt(List<T> data) { if (data == null || data.size() == 0) { return null; } return data.get(0); } }

Guess you like

Origin www.cnblogs.com/anenyang/p/11924245.html