进阶:Object类

2020-04-27  本文已影响0人  雪上霜
//源代码
public String toString(){
    return this.getClass().getName()+"@"+Integer.toHexString(hashCode());
}
public class Test{
    public static void main(String[] args){
        MyTime m = new MyTime();
        m.toString();
        System.out.println(m);
        System.out.println(m.toString());
    }
}

class MyTime{
    int year;
    int month;
    int day;
    
    public MyTime(){
    
    }
    public MyTime(int year,int month,int day){
        this.year = year;
        this.month = month;
        this.day = day;
    }
    public String toString(){
        System.out.println(this.year+"年"+this.month+"月"+this.day+"日");
    }
}
//源代码
public boolean equals(Object obj){
    return (this==obj);
}
public class Test{
    public static void main(String[] args){
        int a = 100;
        int b = 100;
        System.out.println(a==b);
        
        MyTime t1 = new MyTime(2008,8,8);
        MyTime t2 = new MyTime(2008,8,8);
        System.out.println(t1 == t2);//比较的是t1和t2中的值。

        boolean b = t1.equals(t2);
    }
}


class MyTime{
    int year;
    int month;
    int day;
    
    public MyTime(){
    
    }
    public MyTime(int year,int month,int day){
        this.year = year;
        this.month = month;
        this.day = day;
    }
    public boolean equals(Object obj){
        if(obj instanceof MyTime){
            MyTime t = (MyTime)obj;
            if(t.year == this.year && t.month == this.month && t.day == this.day){
            return true;
            }
        }
        return false;
    }
}
public class Test{
    public static void main(String[] args){
        int a = 100;
        int b = 100;
        System.out.println(a==b);
        
        MyTime t1 = new MyTime(2008,8,8);
        MyTime t2 = new MyTime(2008,8,8);
        System.out.println(t1 == t2);//比较的是t1和t2中的值。

        boolean b = t1.equals(t2);
    }
}


class MyTime{
    int year;
    int month;
    int day;
    
    public MyTime(){
    
    }
    public MyTime(int year,int month,int day){
        this.year = year;
        this.month = month;
        this.day = day;
    }
    public boolean equals(Object obj){
        if(obj == null){
            return false;
        }
        if(!(obj instanceof MyTime)){
            return false;
        }
        if(this == obj){
            return true;
        }
        MyTime t = (MyTime)obj;
        if(t.year == this.year && t.month == this.month && t.day == this.day){
            return true;
            }
        return false;
    }
    
    /*
    public boolean equals(Object obj){
        if(obj == null){
            return false;
        }
        if(!(obj instanceof MyTime)){
            return false;
        }
        if(this == obj){
            return true;
        }
        MyTime t = (MyTime)obj;
        return t.year == this.year && t.month == this.month && t.day == this.day;
    }
    */
}
//大部分下这样写
String s = "hello worl";
//其实String也是一个类,不属于基本数据类型,既然String是一个类,那么一定存在构造方法。
String s3 = new String("hello worl");
//比较两个字符串不能使用==。必须使用equals方法。
//String已经重写了toString方法。
public class Test{
    public staic void main(String[] args){
        Address addr = ;
        User u1 = new User("张三",new Address("北京","大兴区","1111"));
        User u2 = new User("张三",new Address("北京","大兴区","1111"));
        
    }
}

class User{
    String name;
    Address addr;
    
    public User(){
    
    }
    public User(String name,Address addr){
        this.name = name;
        this.addr = addr;
    }
    
    public boolean equals(Object obj){
        if(obj == null || !(obj instanceof User))
            return false;
        if(obj == this)
            return true;
        User u = (User)obj;
        if(this.name.equals(u.name) && this.addr.equals(u.addr)){
            return true;
        }
        return false;
    }
}

class Address{
    String city;
    String street;
    String zipcode;
    
    public Address(){
    
    }
    public Address(String city,String street,String zipcode){
        this.city = city;
        this.street = street;
        this.zipcode = zipcode;
    }
    
    public boolean equals(Object obj){
        if(obj == null || !(obj instanceof User))
            return false;
        if(obj == this)
            return true;
        User u = (User)obj;
        if(this.city.equals(u.city) && this.street.equals(u.street) && this.zipcode.equals(u.zipcode)){
            return true;
        }
        return false;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读