• Java 中 泛型的限定


         泛型 一般 出如今集合中,迭代器中 也会出现!
         泛型 是为了 提高代码的 安全性。

    泛型 确保数据类型的唯一性。

    在我们经常使用的容器中。  越是 单一 约优点理啊!  

        泛型的限定:
    ? 是通配符 指代 随意类型

    泛型的限定上限: <? extends E> 接受 E 或者 E 的子类型。

    泛型的限定下限: <?  super   E>  接收  E 或者 E 的父类。

    泛型的限定上限 (定义父类 填装子类 类型。)
      代码:
    package stu.love.v;
    
    import java.util.*;
    
    //泛型限定上限的应用
    
    class Demo12 
    {
    	public static void main(String[] args) 
    	{
    		
    
    		Collection<Student> c = new ArrayList<Student>();
    
    		c.add(new Student("zhaosi",23));
    		c.add(new Student("lisi",25));
    		c.add(new Student("wangwu",20));
         
    	   //TreeSet(Collection<? extends E> c)
    	   // class TreeSet<E>
    	   //{
    	   //}
    		TreeSet<Person> ts = new TreeSet<Person>(c);
    
    		Iterator<Person> ite = ts.iterator();
    		while(ite.hasNext())
    		{
    			sop(ite.next());
    		}
    	}
    	
    	public static void sop(Object obj)
    	{
    		System.out.println(obj);
    	}
    }
    

    泛型的限定下限:(一般用于定义 迭代器: 仅仅需定义 父类类型的迭代器,否则 面向详细的话。扩展性和维护性不好!


    代码:

    package stu.love.v;
    
    import java.util.*;
    
    import stu.love.v.Demo11;
    
    /*// 子类 特殊的 比較器  Student
    class ComByAge implements Comparator<Student>
    {
    	public int compare(Student s1,Student s2)
    	{
    		int num = new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
    		if(num==0)
    			return s1.getName().compareTo(s2.getName());
    		return num;
    	}
    }
    
    // 子类 特殊的 比較器 Teacher
    class ComByAge2 implements Comparator<Teacher>
    {
    	public int compare(Teacher s1,Teacher s2)
    	{
    		int num = new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
    		if(num==0)
    			return s1.getName().compareTo(s2.getName());
    		return num;
    	}
    }*/
    
    //父类的比較器   <Person>
    class CompareByAge implements Comparator<Person>
    {
    	public int compare(Person s1,Person s2)
    	{
    		int num = new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
    		if(num==0)
    			return s1.getName().compareTo(s2.getName());
    		return num;
    	}
    
    }
    
    class Demo13 
    {
    	//TreeSet<E>(Comparator<? super E> comparator) 定义比較器时,能够是E类型,还能够是E的父类型,E在创建集合对象时确定
    	public static void main(String[] args) 
    	{
    		TreeSet<Student> t1 = new TreeSet<Student>(new CompareByAge());
    		t1.add(new Student("zhaosi",23));
    		t1.add(new Student("lisi",25));
    		t1.add(new Student("wangwu",20));
    
    
    		TreeSet<Teacher> t2 = new TreeSet<Teacher>(new CompareByAge());
    		t2.add(new Teacher("wang",38));
    		t2.add(new Teacher("lilaoshi",48));
    		t2.add(new Teacher("zhanglaoshi",58));
    	}
    	
    
    }
    






  • 相关阅读:
    中小型MIS开发之我见(二)具体实施(上)
    实现.NET应用程序的自动更新
    利用XML实现通用WEB报表打印(参考)
    几款开放源代码的软件测试工具介绍
    TextBox只输入数字
    HttpUtility 类 编码,解码字符串为Html字符串
    测试工具的选择和使用
    javascript技巧参考
    认识Web.config文件
    动态改变页面的CSS样式(收藏备用)
  • 原文地址:https://www.cnblogs.com/wzjhoutai/p/6866465.html
Copyright © 2020-2023  润新知