Object

(1)Object:java中所有類的父類
來自於:java.lang.object
        java中不允許循環繼承
        class A extends B
        Class B extends A
(2)方法:11個方法
    (1)getClass() 返回對象的類對象 常用來比較兩個對象是否是同一類型
    (2)finalize() 對象被回收時調用,有垃圾回收器自動調用。
    垃圾回收:垃圾過多時,會造成內存泄露等問題
              垃圾回收器在內存即將耗盡時通過零引用算法認定垃圾對象。
        程序猿可以通過System.GC()手動調用資源回收器回收垃圾
    (3)toString() 返回對象的字符串形式
    (4)equals() 判斷兩個對象是否內容相同
    比較固定的寫法:
    public boolean equals(Object o){
        if(this == o)return true;
        if(o==null)return false;
        if(this.getClass()!=o.getClass())return false;
        本類型 變量名 = (類型)o;//強轉
        判斷內容相同
    }
    (5)int hashcode()object中默認通過引用的地址計算哈希值, 儘量保證內容不同的對象返回不同的int
    (6)這是5個方法 wait()和notify() 兩種組合使線程之間能夠通信
    o.wait(): 必須放在對o加鎖的同步代碼塊中. 線程會釋放其所擁有的所有鎖標記,進入o的等待隊列
o.notify()/notifyAll():必須放在對o加鎖的同步代碼塊中,線程會從o的等待隊列中釋放一個/全部線程
    (7)clone()
(3)hashcode和equals方法
解決的問題:hashSet和ArrayList中,現在要放入很多元素,可能有相同的元素,其中hashSet是不要求放入相同元素的。這是hashcode的算法和equals的實現就是最有效解決這個問題。
hashcode可以根據一個對象的地址計算對象一個哈希值,可以理解爲放到一個區域中,如果再有對象也計算出這個哈希值,再比較這個對象和已有對象的equals方法。這兩個方法共同決定
兩個對象是否相同。
這個情況的應用:儘量保證內容不同的對象比較出來,我們要重載這兩個方法。
有個情況可以很好的解釋:remove方法的使用。如果我們將一個對象放入集合中,這是應該是有一個對象的,如果我們改變了參與hashCode值計算的屬性,這時我們將這個對象通過remove移除可能就無法移除了,但我們不知道會造成內存泄露(這個內存怎麼消失了??就是因爲一個對象一直沒有被釋放)。
public class TestReflection {
    public void reflectionApp(){
        Collection<ReflectionPoint> collections = new ArrayList<ReflectionPoint>();
        ReflectionPoint rfctp1 =new ReflectionPoint(3,3);
        ReflectionPoint rfctp2 =new ReflectionPoint(4,4);
        ReflectionPoint rfctp3 =new ReflectionPoint(5,5);
        ReflectionPoint rfctp4 =new ReflectionPoint(3,3);
        collections.add(rfctp1);
        collections.add(rfctp2);
        collections.add(rfctp3);
        collections.add(rfctp4);
        collections.add(rfctp1);
        System.out.println("ArrayList");
        for(ReflectionPoint rfctp:collections){
            System.out.println(rfctp);
        }
        collections = new HashSet<ReflectionPoint>();
        collections.add(rfctp1);
        collections.add(rfctp2);
        collections.add(rfctp3);
        collections.add(rfctp4);
        collections.add(rfctp1);
        System.out.println("HashSet");
        for(ReflectionPoint rfctp:collections){
            System.out.println(rfctp);
        }
    }
}

這個類寫的就不會將rfctp4放進去。public class TestReflection {
    public void reflectionApp(){
        Collection<ReflectionPoint> collections = new ArrayList<ReflectionPoint>();
        ReflectionPoint rfctp1 =new ReflectionPoint(3,3);
        ReflectionPoint rfctp2 =new ReflectionPoint(4,4);
        ReflectionPoint rfctp3 =new ReflectionPoint(5,5);
        ReflectionPoint rfctp4 =new ReflectionPoint(3,3);
        collections.add(rfctp1);
        collections.add(rfctp2);
        collections.add(rfctp3);
        collections.add(rfctp4);
        collections.add(rfctp1);
        System.out.println("ArrayList");
        for(ReflectionPoint rfctp:collections){
            System.out.println(rfctp);
        }
        collections = new HashSet<ReflectionPoint>();
        collections.add(rfctp1);
        collections.add(rfctp2);
        collections.add(rfctp3);
        collections.add(rfctp4);
        collections.add(rfctp1);
        System.out.println("HashSet");
        for(ReflectionPoint rfctp:collections){
            System.out.println(rfctp);
        }
    }
}

class ReflectionPoint{
    //屬性
    private double x;
    private double y;
    //構造方法
    public ReflectionPoint(){}
    public ReflectionPoint(double x,double y){
        this.x = x;
        this.y = y;
    }
    //set和get方法
    public double getX(){return x;}
    public double getY(){return y;}
    public void setX(double x){this.x = x;}
    public void getY(double y){this.y = y;}
    
    //toString
    public String toString(){
        return "X = " + x + " Y = " + y;
    }
    
    //equals
    public boolean equals(Object o){
        if(this == o) return true;
        if(o == null) return false;
        if(this.getClass()!=o.getClass())return false;
        ReflectionPoint rf = (ReflectionPoint)o;
        if(rf.getX()!=x)return false;
        if(rf.getY()!=y)return false;
        if(rf.getX()!=x)return false;
        if(rf.getY()!=y)return false;
        return true;
    }
    
    //hashcode
    public int hashCode(){
        return (int)(x+y);
    }
}


ArrayList
X = 3.0 Y = 3.0
X = 3.0 Y = 4.0
X = 3.0 Y = 5.0
X = 3.0 Y = 3.0
X = 3.0 Y = 3.0
 equals 3.0+4.0
 equals 3.0+5.0
 equals 3.0+3.0
HashSet
X = 3.0 Y = 3.0
這個我理解是:無論你是否重載了hashCode系統都會爲你對對象的地址進行一次匹配(引用),如果你重載了之後它纔會執行hashCode和equals (三步)但移除的時候是直接執行我們自己重載的hashCode。(兩步)
public class TestReflection {
    public void reflectionApp(){
        Collection<ReflectionPoint> collections = new ArrayList<ReflectionPoint>();
        ReflectionPoint rfctp1 =new ReflectionPoint(3,3);
        ReflectionPoint rfctp2 =new ReflectionPoint(3,4);
        ReflectionPoint rfctp3 =new ReflectionPoint(3,5);
        ReflectionPoint rfctp4 =new ReflectionPoint(3,3);
        collections.add(rfctp1);
        collections.add(rfctp2);
        collections.add(rfctp3);
        collections.add(rfctp4);
        collections.add(rfctp1);
        System.out.println("ArrayList");
        for(ReflectionPoint rfctp:collections){
            System.out.println(rfctp);
        }
        collections = new HashSet<ReflectionPoint>();
        collections.add(rfctp1);
        collections.add(rfctp2);
        collections.add(rfctp3);
        collections.add(rfctp4);
        collections.add(rfctp1);
        System.out.println("HashSet");
        for(ReflectionPoint rfctp:collections){
            System.out.println(rfctp);
        }
    }
}

class ReflectionPoint{
    //屬性
    private double x;
    private double y;
    //構造方法
    public ReflectionPoint(){}
    public ReflectionPoint(double x,double y){
        this.x = x;
        this.y = y;
    }
    //set和get方法
    public double getX(){return x;}
    public double getY(){return y;}
    public void setX(double x){this.x = x;}
    public void getY(double y){this.y = y;}
    
    //toString
    public String toString(){
        return "X = " + x + " Y = " + y;
    }
    
    //equals
    public boolean equals(Object o){
        /*if(this == o) return true;
        if(o == null) return false;
        if(this.getClass()!=o.getClass())return false;
        ReflectionPoint rf = (ReflectionPoint)o;
        if(rf.getX()!=x)return false;
        if(rf.getY()!=y)return false;
        if(rf.getX()!=x)return false;
        if(rf.getY()!=y)return false;*/
        System.out.println("equals");
        return true;
    }
    
    //hashcode
    public int hashCode(){
        return (int)x;
    }
}




發佈了33 篇原創文章 · 獲贊 1 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章