JAVA#通配符练习--1

import org.junit.Test;

import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class Employee implements Comparable{

    public static void main(String arg[]){
        Employee e1=new Employee("宋毅铭",8,new Mydate(1996,11,11));
        Employee e2=new Employee("宋艺华",88,new Mydate(1995,11,19));
        Employee e3=new Employee("宋西青",888,new Mydate(1996,1,11));
        Employee e4=new Employee("宋香茗",8888,new Mydate(1996,12,11));
//通配符
        TreeSet set1=new TreeSet();
        set1.add(e1);
        set1.add(e2);
        set1.add(e3);
        set1.add(e4);
        Iterator i=set1.iterator();
        while (i.hasNext()){
            System.out.println(i.next());

    }

    }
    private String name;
    private Integer age;
    private Mydate birthday;

    public Employee(){
    }
    public Employee(String name,Integer age, Mydate birthday){
        this.name=name;
        this.age=age;
        this.birthday=birthday;
    }
    public void setName(String name){
        this.name=name;
    }
    public void setAge(int age){
        this.age=age;
    }

    public void setBirthday(Mydate birthday) {
        this.birthday = birthday;
    }

    public Integer getAge() {
        return age;
    }

    public Mydate getBirthday() {
        return birthday;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "Name["+this.getName()+"] Age["+this.getAge()+"] Birthday["+this.getBirthday()+"]";
    }

    @Override
    public int compareTo(Object o) {
        if(o instanceof Employee){
            Employee mm=(Employee) o;
            //this.getAge()返回的是
            int num=this.getAge().compareTo(mm.getAge());
            if(num==0){
                return this.getName().compareTo(mm.getName());
            }
            else
                return num;
        }
        return 0;
    }


    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Employee mm = (Employee) obj;
        if (name == null) {
            if (mm.name != null)
                return false;
        } else if (!name.equals(mm.name))
            return false;
        if (age != mm.age)
            return false;
        if(birthday!=mm.birthday)
            return false;

        return true;
    }

    @Override
    public int hashCode() {
        final int kobe = 31;
        int iverson = 3;
        iverson = kobe * iverson + age+((birthday==null)?0:birthday.hashCode());
        iverson = kobe * iverson + ((name == null) ? 0 : name.hashCode());
        return iverson;
    }
}
class Mydate{
    private int month;
    private int day;
    private int year;
    public Mydate(int year,int month,int day){
        this.day=day;
        this.month=month;
        this.year=year;
    }
    public void setDay(int day) {
        this.day = day;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getDay() {
        return day;
    }

    public int getMonth() {
        return month;
    }

    public int getYear() {
        return year;
    }

    @Override
    public String toString() {
        return this.getYear()+" "+this.getMonth()+" "+this.getDay();
    }
}
Name[宋毅铭] Age[8] Birthday[1996 11 11]
Name[宋艺华] Age[88] Birthday[1995 11 19]
Name[宋西青] Age[888] Birthday[1996 1 11]
Name[宋香茗] Age[8888] Birthday[1996 12 11]

 

你可能感兴趣的:(Java)