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;
}
}
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;
}
}
相关文章推荐
- JAVA容器类
- java基础巩固笔记(4)-代理
- java基础巩固笔记(3)-类加载器
- Java基础笔记汇总
- Spring MVC之一 HelloWorld
- Java byte 数组转换 16 进制字符串
- 2016/2/13 Head First Java 第7节第一部分小结 继承
- Eclipse快捷键大全
- Spark学习笔记8-在eclipse里用Java编写spark程序
- double.class与Double.class
- struts2 工作原理(仅图片说明)
- JNI:使用RegisterNatives方法传递和使用Java自定义类
- 奇葩问题 eclipse中DDMS的LOGcat只有一列level
- 查看Eclipse版本号,及各个版本区别
- 查看Eclipse版本号,及各个版本区别
- javaWeb笔记(六) 分页 显示分页页码列表
- java正则表达式提取xxxx(yyyy)中的内容
- Javaio
- java基础学习笔记之类加载器
- Java设计模式(二十三)----解释器模式