您的位置:首页 > 编程语言 > Java开发

java 泛型学习

2016-02-13 16:00 447 查看
1、学习泛型类、泛型方法、泛型参数

2、泛型的上限、下限

package IMF;

public class JavaGenerics {

public static void main(String[] args) {

//Empty<String> empty =null;

//empty = new Empty<String>("spark");

//System.out.println("Content: "+ empty.getItem());

//Empty<String,Integer> empty =null;

//empty = new Empty<String,Integer>("Spark",6);

//empty = new Empty ("Spark",99.9999);

//System.out.println(empty.getKey() + "->" + empty.getValue());

//log(empty);

/*

News<String> news=null;

news=new ITNews<String>("Life is short,you need spak!");

System.out.println(news.getContent());

*/

Integer[] data ={1,2,3,4,5,6,7,8,9};

arrayGenerics(data);

}

private static <T> void arrayGenerics(T[] data) {

for(T item:data){

System.out.println(item);

}

}

private static void log(Empty<? extends String, ? extends Number> empty) {

System.out.println(empty.getKey() + "->" + empty.getValue());

}

}

interface News<T>{

public T getContent();

}

class ITNews<T> implements News<T>{

private T content;

@Override

public T getContent() {

// TODO Auto-generated method stub

return this.content;

}

public void setContent(T content) {

this.content = content;

}

public ITNews(T content) {

super();

this.content = content;

}

}

class Empty<K extends String,V extends Number>{

private K key ;

private V value;

public Empty(K key, V value) {

super();

this.key = key;

this.value = value;

}

public K getKey() {

return key;

}

public void setKey(K key) {

this.key = key;

}

public V getValue() {

return value;

}

public void setValue(V value) {

this.value = value;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: