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

java基础学习之泛型 九-4

2017-02-27 18:25 309 查看
* 泛型格式:

 * <数据类型>

 * 泛型有点:

 * 解决黄色警告线问题

 * 把运行期间的类型转换异常提前到了编译期间

 * 优化程序设计

简单使用

package fanxing;

import java.util.ArrayList;
import java.util.Iterator;
/**
* 泛型
* @author Angus
*	泛型格式:
*		<数据类型>
*	泛型有点:
*		解决黄色警告线问题
*		把运行期间的类型转换异常提前到了编译期间
*		优化程序设计
*/

public class ArrayListDemo {

public static void main(String[] args) {
//用ArrayList存储字符串遍历
//方式一
ArrayList array = new ArrayList();
array.add("hello");
array.add("hello");
array.add("hello");

Iterator it = array.iterator();
while(it.hasNext()){
String s = (String) it.next();
System.out.println(s);
}
System.out.println("------------------");

//		ArrayList array2 = new ArrayList();
//		array.add("hello");
//		array.add("hello");
//		array.add("hello");
//		array.add(10); //可以存储int类型 ,但是这样是不符合逻辑的,遍历会出错

//所以在存储的时候要有限制类型,这样就用到了泛型

ArrayList<String> array3 = new ArrayList<String>();

array3.add("hello");
array3.add("hello");
array3.add("hello");

Iterator<String> it2 = array.iterator();
while(it2.hasNext()){
String s = it2.next();
System.out.println(s);
}

}

}


泛型类

package fanxing;

public class Tool {
public void show(String str){
System.out.println(str);

}
public void show(int str){
System.out.println(str);

}
}


测试代码;

package fanxing;
/**
* 泛型类
* @author Angus
*
*/
public class ToolTest {
public static void main(String[] args) {
Tool t = new Tool();
t.show("hehe");
t.show(10); //报错需要方法重载

//		//问题  能不能不用方法重载实现?
//		Tool2 t2 = new Tool2();
//		t2.show("hello");
//		t2.show(10);
//
//		//改进
//		Tool2<String> t3 = new Tool2<String>();
//		t3.show("hah");

}
}
解决不使用重载

package fanxing;
/**
* 泛型类,把泛型定义在类上
* @author Angus
*
*/
public class Tool2<E> {
public void show(E e){
System.out.println(e);
}
}

package fanxing;
/**
* 泛型类
* @author Angus
*
*/
public class ToolTest {
public static void main(String[] args) {
//		Tool t = new Tool();
//		t.show("hehe");
//		t.show(10); //报错需要方法重载

//问题  能不能不用方法重载实现?
Tool2 t2 = new Tool2();
t2.show("hello");
t2.show(10);

//改进
Tool2<String> t3 = new Tool2<String>();
t3.show("hah");

}
}
这样就解决了不需要重载方法可以实现多个类型的输出。。。。

泛型方法:

package fanxing;
/**
* 泛型方法
* @author Angus
*	为了保证方法传递不同参数,在类上 明确了类型
*	这样要是在调用方法的时候再赋值泛型多好呢?
*
*/
public class Tool{
//把泛型加在方法中使用
public<E>void show(E e){
System.out.println(e);

}
}


测试;

package fanxing;
/**
* 泛型
* @author Angus
*
*/
public class ToolTest {
public static void main(String[] args) {
Tool t = new Tool();
t.show("hehe");
t.show(10);
}
}


不报错了,结果;



泛型接口

定义接口:

package fanxing;

public interface inter <E>{
public abstract void show(E e);
}


实现类
package fanxing;
/**
*
* @author Angus
* @param <E>
*
* @param <E>
* 泛型接口
* 如果一个接口上有泛型,在实现类的时候需要什么类型,写上什么类型
* 如果需要任何类型就这样写上泛型类
*/
//public class interimpl implements inter<String> {
public class interimpl<E> implements inter<E> {

@Override
public void show(E e) {
System.out.println(e);
}

//	@Override
//	public void show(String e) {
//		System.out.println(e);
//	}

}


package fanxing;
/**
* 泛型接口测试
* @author Angus
*
*/
public class InterTest {
public static void main(String[] args) {
//		interimpl ii = new interimpl();
//		ii.show("呵呵");
interimpl<String> il = new interimpl<String>();
il.show("哈哈");
}
}


运行没有问题。。。。

最后附上JDK使用文档API 下载

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