普通泛型
- class Point<T>{
- private T var ;
- public T getVar(){
- return var ;
- }
- public void setVar(T var){
- this.var = var ;
- }
- };
- public class GenericsDemo06{
- public static void main(String args[]){
- Point<String> p = new Point<String>() ;
- p.setVar("it") ;
- System.out.println(p.getVar().length()) ;
- }
- };
- ----------------------------------------------------------
- class Notepad<K,V>{
- private K key ;
- private V value ;
- public K getKey(){
- return this.key ;
- }
- public V getValue(){
- return this.value ;
- }
- public void setKey(K key){
- this.key = key ;
- }
- public void setValue(V value){
- this.value = value ;
- }
- };
- public class GenericsDemo09{
- public static void main(String args[]){
- Notepad<String,Integer> t = null ;
- t = new Notepad<String,Integer>() ;
- t.setKey("汤姆") ;
- t.setValue(20) ;
- System.out.print("姓名;" + t.getKey()) ;
- System.out.print(",年龄;" + t.getValue()) ;
-
- }
- };
通配符
- class Info<T>{
- private T var ;
- public void setVar(T var){
- this.var = var ;
- }
- public T getVar(){
- return this.var ;
- }
- public String toString(){
- return this.var.toString() ;
- }
- };
- public class GenericsDemo14{
- public static void main(String args[]){
- Info<String> i = new Info<String>() ;
- i.setVar("it") ;
- fun(i) ;
- }
- public static void fun(Info<?> temp){
- System.out.println("内容:" + temp) ;
- }
- };
受限泛型
- class Info<T>{
- private T var ;
- public void setVar(T var){
- this.var = var ;
- }
- public T getVar(){
- return this.var ;
- }
- public String toString(){
- return this.var.toString() ;
- }
- };
- public class GenericsDemo17{
- public static void main(String args[]){
- Info<Integer> i1 = new Info<Integer>() ;
- Info<Float> i2 = new Info<Float>() ;
- i1.setVar(30) ;
- i2.setVar(30.1f) ;
- fun(i1) ;
- fun(i2) ;
- }
- public static void fun(Info<? extends Number> temp){
- System.out.print(temp + "、") ;
- }
- };
- ----------------------------------------------------------
- class Info<T>{
- private T var ;
- public void setVar(T var){
- this.var = var ;
- }
- public T getVar(){
- return this.var ;
- }
- public String toString(){
- return this.var.toString() ;
- }
- };
- public class GenericsDemo21{
- public static void main(String args[]){
- Info<String> i1 = new Info<String>() ;
- Info<Object> i2 = new Info<Object>() ;
- i1.setVar("hello") ;
- i2.setVar(new Object()) ;
- fun(i1) ;
- fun(i2) ;
- }
- public static void fun(Info<? super String> temp){
- System.out.print(temp + "、") ;
- }
- };
泛型无法向上转型
- class Info<T>{
- private T var ;
- public void setVar(T var){
- this.var = var ;
- }
- public T getVar(){
- return this.var ;
- }
- public String toString(){
- return this.var.toString() ;
- }
- };
- public class GenericsDemo23{
- public static void main(String args[]){
- Info<String> i1 = new Info<String>() ;
- Info<Object> i2 = null ;
- i2 = i1 ;
- }
- };
泛型接口
- interface Info<T>{
- public T getVar() ;
- }
- class InfoImpl<T> implements Info<T>{
- private T var ;
- public InfoImpl(T var){
- this.setVar(var) ;
- }
- public void setVar(T var){
- this.var = var ;
- }
- public T getVar(){
- return this.var ;
- }
- };
- public class GenericsDemo24{
- public static void main(String arsg[]){
- Info<String> i = null;
- i = new InfoImpl<String>("汤姆") ;
- System.out.println("内容:" + i.getVar()) ;
- }
- };
- ----------------------------------------------------------
- interface Info<T>{
- public T getVar() ;
- }
- class InfoImpl implements Info<String>{
- private String var ;
- public InfoImpl(String var){
- this.setVar(var) ;
- }
- public void setVar(String var){
- this.var = var ;
- }
- public String getVar(){
- return this.var ;
- }
- };
- public class GenericsDemo25{
- public static void main(String arsg[]){
- Info i = null;
- i = new InfoImpl("汤姆") ;
- System.out.println("内容:" + i.getVar()) ;
- }
- };
泛型方法
- class Demo{
- public <T> T fun(T t){
- return t ;
- }
- };
- public class GenericsDemo26{
- public static void main(String args[]){
- Demo d = new Demo() ;
- String str = d.fun("汤姆") ;
- int i = d.fun(30) ;
- System.out.println(str) ;
- System.out.println(i) ;
- }
- };
通过泛型方法返回泛型类型实例
- class Info<T extends Number>{
- private T var ;
- public T getVar(){
- return this.var ;
- }
- public void setVar(T var){
- this.var = var ;
- }
- public String toString(){
- return this.var.toString() ;
- }
- };
- public class GenericsDemo27{
- public static void main(String args[]){
- Info<Integer> i = fun(30) ;
- System.out.println(i.getVar()) ;
- }
- public static <T extends Number> Info<T> fun(T param){
- Info<T> temp = new Info<T>() ;
- temp.setVar(param) ;
- return temp ;
- }
- };
使用泛型统一传入的参数类型
- class Info<T>{
- private T var ;
- public T getVar(){
- return this.var ;
- }
- public void setVar(T var){
- this.var = var ;
- }
- public String toString(){
- return this.var.toString() ;
- }
- };
- public class GenericsDemo28{
- public static void main(String args[]){
- Info<String> i1 = new Info<String>() ;
- Info<String> i2 = new Info<String>() ;
- i1.setVar("HELLO") ;
- i2.setVar("汤姆") ;
- add(i1,i2) ;
- }
- public static <T> void add(Info<T> i1,Info<T> i2){
- System.out.println(i1.getVar() + " " + i2.getVar()) ;
- }
- };
泛型数组
- public class GenericsDemo30{
- public static void main(String args[]){
- Integer i[] = fun1(1,2,3,4,5,6) ;
- fun2(i) ;
- }
- public static <T> T[] fun1(T...arg){
- return arg ;
- }
- public static <T> void fun2(T param[]){
- System.out.print("接收泛型数组:") ;
- for(T t:param){
- System.out.print(t + "、") ;
- }
- }
- };
泛型的嵌套设置
- class Info<T,V>{
- private T var ;
- private V value ;
- public Info(T var,V value){
- this.setVar(var) ;
- this.setValue(value) ;
- }
- public void setVar(T var){
- this.var = var ;
- }
- public void setValue(V value){
- this.value = value ;
- }
- public T getVar(){
- return this.var ;
- }
- public V getValue(){
- return this.value ;
- }
- };
- class Demo<S>{
- private S info ;
- public Demo(S info){
- this.setInfo(info) ;
- }
- public void setInfo(S info){
- this.info = info ;
- }
- public S getInfo(){
- return this.info ;
- }
- };
- public class GenericsDemo31{
- public static void main(String args[]){
- Demo<Info<String,Integer>> d = null ;
- Info<String,Integer> i = null ;
- i = new Info<String,Integer>("汤姆",30) ;
- d = new Demo<Info<String,Integer>>(i) ;
- System.out.println("内容一:" + d.getInfo().getVar()) ;
- System.out.println("内容二:" + d.getInfo().getValue()) ;
- }
- };
泛型方法不一定要通过参数来确定泛型准确类型,可以只通过返回值,比如:
public static <E> ArrayList<E> newArrayList() {
return new ArrayList<E>();
}
public List<PrepaidHistory> queryHistories(Long skyid,PrepaidHistoryType type, Date from, Date end) {
。。。
return Lists.newArrayList();
}
这样Lists.newArrayList();
智能的知道返回类型为PrepaidHistory
发表评论 取消回复