zoukankan      html  css  js  c++  java
  • Day14 自己定义泛型类的使用

    泛型的引入和体现:

    问题:集合中能够存储各种类型的元素,可是由于集合能够存储各种类型数据。在获取集合中元素时,就会造成数据不安全。

    public class GenericDemo {
    	
    	public static void main(String []str)
    	{
    		List list = new ArrayList();
    		list.add(10);//自己主动装箱
    		list.add(new Integer(100));
    		list.add(Integer.valueOf(1000));
    		
    		for(Object obj:list)
    			System.out.println(obj);
    	}
    
    }

    在开发中,能够会发生一种情况:用户输入了一个数据并存储到集合中。可是用户可能输入String类型的数据


    public static void main(String []str)
    	{
    		List list = new ArrayList();
    		list.add("10");
    		list.add(new Integer(100));
    		list.add(Integer.valueOf(1000));
    		
    		for(Object obj:list)
    		{
    			Integer num = (Integer)obj;
    			System.out.println(num);
    		}
    			
    	}

    Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
        at cn.itcast.test.GenericDemo.main(GenericDemo.java:17)

    解决方式:能够使用instanceofkeyword避免ClassCastException异常

              在JDK1.5后。出现了一种新的解决方式:泛型


    思考:假设集合中仅仅能存储一个类型的元素,还会出现获取数据时不安全的问题吗?

                实现方式:在定义集合时明白了该集合中能够存储元素的类型


    泛型的使用格式:   <引用类型>    注意:泛型中仅仅能使用引用类型


    package org.test;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class TestGeneric {
    	public static void main(String[] args) {
    		//自己定义泛型类的使用
    		//1.实例化泛型类的对象时指明泛型的类型,这儿不能一般类型,仅仅能是引用类型
    		//全部使用了泛型类型的地方。都要变成泛型类的类型
    		//2.假设不指明类型,默认是Object类型
    		order<Integer> o = new order<Integer>();
    		
    		//order oo = new order();//假设不指定泛型的类型,默认是Object类型
    		
    		List<Integer>list = o.getList();
    		list.add(10);
    		list.add(11);
    		
    		System.out.println(o.getList());
    		
    		Integer i = o.getE(100);
    		System.out.println(i);
    	}
    	
    }
    
    
    class order<T>
    {
    	int MaxSize = 30;
    	//public T []t = new T[MaxSize];   没有泛型数组
    	private T t;
    	private List<T> list = new ArrayList<T>();
    	
    	public List<T> getList()
    	{
    		return list;
    	}
    	//声明泛型方法  <E>一定要在权限修饰符后面。不能缺少
    	public <E> E getE(E e)
    	{
    		return e;
    	}
    	
    	
    }
    //子类还是不确定类型T ,实例化子类的对象时候要指明泛型的引用类型
    
    //继承泛型类或泛型接口时,能够指明泛型的类型 class SubOrder<Integer> extends order<Integer>
    class SubOrder <T>extends order<T>
    {
    	
    }

    泛型的优点?

    1,  攻克了集合中存储数据的不安全性

    2,  把执行时可能发生的异常,放在编译时作为编译错误处理了,避免了执行时的异常

    3。  省略了代码中的强制类型转换的书写

    @Test
    	public void Demo2()
    	{
    		List<Integer> list = new ArrayList<Integer>();
    		list.add(10);
    		list.add(new Integer(100));
    		list.add(Integer.valueOf(1000));
    		
    		//使用迭代器遍历集合。在迭代器中明白要迭代的类型
    		for (Iterator<Integer> it = list.iterator(); it.hasNext();) {
    			//由于迭代器明白了要迭代的类型。所以不须要强制类型转换
    			//Integer num = (Integer)it.next();
    			//不须要强制类型转换
    			Integer num = it.next();
    			System.out.println(num);
    		}
    		//使用foreach遍历集合
    		//由于list集合中明白了集合的类型,所以在foreach中能够直接声明为集合中的元素类型,而不是之前的Object
    		for(Integer i:list)
    		{
    			System.out.println(i);
    		}
    			
    	}


  • 相关阅读:
    openssl 自己制作ssl证书:自己签发免费ssl证书,为nginx生成自签名ssl证书
    【nginx】一台nginx服务器多域名配置
    使用 Microsoft.Web.Administration 管理iis
    Android TextView : “Do not concatenate text displayed with setText”
    Android中将十六进制 颜色代码 转换为int类型数值
    android-getTextSize返回值是以像素(px)为单位的,setTextSize()以sp为单位
    Android属性allowBackup安全风险浅析
    Android 如何保持屏幕常亮
    Android 控制ScrollView滚动到底部
    Android 自定义TextView 实现文本间距
  • 原文地址:https://www.cnblogs.com/yxwkf/p/5405965.html
Copyright © 2011-2022 走看看