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学习基础加强之泛型
- 黑马程序员—java基础学习--集合类、泛型
- java基础学习之泛型
- JavaWeb学习笔记-java基础-10-泛型
- Java基础学习笔记之泛型
- 黑马程序员java学习<基础加强>—泛型
- java基础学习笔记——泛型代码详解
- java基础学习之泛型
- 黑马程序员—Java基础加强学习笔记之泛型
- Java基础学习笔记【08】泛型限定、泛型操作
- java学习基础泛型和其他对象
- Java基础学习之泛型(泛型方法、泛型类、通配符的使用)
- Java基础学习笔记(九)Comparable接口、Map接口、泛型
- 黑马程序员--集合框架知识总结和泛型--java学习日记7(基础知识)
- 黑马程序员--Java基础学习笔记【集合-List、泛型】
- JAVA基础学习day15--集合二 TreeSet和泛型
- Java 深入学习(29) —— 泛型基础
- [学习笔记] Java核心技术 卷一:基础知识 泛型程序设计(五)
- 【幻化万千戏红尘】qianfengDay17-java基础学习:泛型,路径,File类,IO流
- Java基础学习——泛型(generics)学习一