zoukankan      html  css  js  c++  java
  • Java基础之泛型限定的使用示例:

    import java.util.*;
    class GenericDemo
    {
        public static void main(String[] args)
        {
            TreeSet<Person> ts = new TreeSet<Person>(new MyComparator());
            
            ts.add(new Person("张三","男",28,175));
            ts.add(new Person("李四","女",28,160));
            ts.add(new Person("王五","女",27,185));
            ts.add(new Person("麻六","男",38,174));
            
            PrintSet(ts);
            
            TreeSet<Student> stu = new TreeSet<Student>(new MyComparator());
            
            stu.add(new Student("张三","男",28,175,8.9));
            stu.add(new Student("李四","女",28,160,8.8));
            stu.add(new Student("王五","女",27,185,8.8));
            stu.add(new Student("麻六","男",38,174,9.0));
            
            PrintSet(stu);
            
            TreeSet<Worker> wk = new TreeSet<Worker>(new MyComparator());
            
            wk.add(new Worker("张三","男",28,175,8.92));
            wk.add(new Worker("李四","女",28,160,8.83));
            wk.add(new Worker("王五","女",27,185,8.84));
            wk.add(new Worker("麻六","男",38,174,9.05));
            
            PrintSet(wk);
        }
        
        public static void PrintSet(TreeSet<? extends Person> set)
        {
            for(Iterator<? extends Person> it = set.iterator();it.hasNext();)
            {
                Person p = it.next();
                System.out.println(p.getName()+"\t"+p.getSex()+"\t"+p.getAge()+"\t"+p.getHeight());
            }
        }
        
        /*
        public static void PrintSet(TreeSet<? super Student> set)
        {
            for(Iterator<? super Student> it = set.iterator();it.hasNext();)
            {
                Person p = (Person)it.next();
                System.out.println(p.getName()+"\t"+p.getSex()+"\t"+p.getAge()+"\t"+p.getHeight());
            }
        }
        
    */
    }

    class MyComparator implements Comparator<Person> 
    {
        public int compare(Person p1,Person p2)
        {
            if(p2.getName().equals(p1.getName()))
            {
                if(p2.getAge()==p1.getAge())
                {
                    return 0;
                }
            }
            
            return p2.getName().compareTo(p1.getName());
        }
    }

    class Person implements Comparable
    {
        private String name;
        private String sex;
        private int age;
        private int height;
        
        public Person(String name,String sex,int age,int height)
        {
            this.name = name;
            this.sex = sex;
            this.age = age;
            this.height = height;
        }
        
        public void setName(String name)
        {
            this.name = name;
        }
        
        public String getName()
        {
            return this.name;
        }
        
        public void setSex(String sex)
        {
            this.sex = sex;
        }
        
        public String getSex()
        {
            return this.sex;
        }
        
        public void setAge(int age)
        {
            this.age = age;
        }
        
        public int getAge()
        {
            return this.age;
        }
        
        public void setHeight(int height)
        {
            this.height = height;
        }
        
        public int getHeight()
        {
            return this.height;
        }
        
        public int compareTo(Object obj)
        {
            if(!(obj instanceof Person))
                return -1;
                
            Person p = (Person)obj;
            int number = this.getName().compareTo(p.getName());
            if(number==0)
            {
                return this.getAge() - p.getAge();
            }
            
            return number;
        }
        
        public int hashCode()
        {
            return this.getName().hashCode()+this.getAge()*21;
        }
        
        public boolean equals(Object obj)
        {
            if(!(obj instanceof Person))
                return false;
                
            Person p = (Person)obj;
            
            return (p.getName().equals(this.getName()) && p.getAge()==this.getAge());
        }
    }

    class Student extends Person
    {
        private double score;
        public Student(String name,String sex,int age,int height,double score)
        {
            super(name,sex,age,height);
            this.score = score;
        }
        
        public void setScore(double score)
        {
            this.score = score;
        }
        
        public double getScore()
        {
            return this.score;
        }
    }

    class Worker extends Person
    {
        private double pay;
        public Worker(String name,String sex,int age,int height,double pay){
            super(name,sex,age,height);
            this.pay = pay;
        }
        
        public void setPay(double pay)
        {
            this.pay = pay;
        }
        
        public double getPay()
        {
            return this.pay;
        }
    }
  • 相关阅读:
    命令行中邮件的收发
    关于location对象
    正则表达式
    一家初创公司的 CTO 应当做什么?
    移动数据网络质量的国家奖牌榜
    MFQ&PPDCS测试分析和测试设计框架l学习记录
    Python学习笔记之基本语法学习1
    《用Python做HTTP接口测试》学习感悟
    我的中台的理解
    中台与平台的区别
  • 原文地址:https://www.cnblogs.com/cxmsky/p/2864258.html
Copyright © 2011-2022 走看看