java的object中有哪些方法

面試的時候只想到了equal() hashcode() tostring()
面試官當時問equal()和 hashcode()區(qū)別 我的回答是:equal是否通過比較地址判斷對象是否相等,hashcode是通過hash值判斷對象是否相等
很長時間沒有看過這些基礎(chǔ)知識了 突然問還是不會的,
————————————————————————————————
下面是打開studio查的object的方法

public class Object {
    public Object() {
        throw new RuntimeException("Stub!");
    }

    @RecentlyNonNull
    public final Class<?> getClass() {
        throw new RuntimeException("Stub!");
    }

    public int hashCode() {
        throw new RuntimeException("Stub!");
    }

    public boolean equals(@RecentlyNullable Object obj) {
        throw new RuntimeException("Stub!");
    }

    @RecentlyNonNull
    protected Object clone() throws CloneNotSupportedException {
        throw new RuntimeException("Stub!");
    }

    @RecentlyNonNull
    public String toString() {
        throw new RuntimeException("Stub!");
    }

    public final native void notify();

    public final native void notifyAll();

    public final void wait(long timeout) throws InterruptedException {
        throw new RuntimeException("Stub!");
    }

    public final native void wait(long var1, int var3) throws InterruptedException;

    public final void wait() throws InterruptedException {
        throw new RuntimeException("Stub!");
    }

    protected void finalize() throws Throwable {
        throw new RuntimeException("Stub!");
    }
}

復(fù)盤面試官的問題:
1.object常用的方法,除了equals、 hashcode 和tostring,剩下的notify()、notifyAll()和wait() 全是線程用的方法
2.getClass是獲取對象的字節(jié)碼類型,得到該對象的真實類型,該方法屬于java的反射機制,返回的java的class類型,例如:Class cl=obj.getClass(),通過cl對象,我們可以獲取該對象的所有成員方法,每個成員方法都是一個method對象。我們也可以獲得該對象的成員變量,每個對象都是一個Filed對象,同樣也可以獲得該對象的構(gòu)造函數(shù)

寫了點關(guān)于反射的測試代碼
Person類(userName和age采用不同的修飾符是為了查看getFileds,getMethods和getDeclaredFields,etDeclaredMethods的區(qū)別 )

public class Person {
    public String userName;
    private int age;

    public Person(String userName, int age) {
        this.userName = userName;
        this.age = age;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

反射寫的代碼 我直接在activity中測試的

    Class<Person> c = Person.class;//獲取person對應(yīng)的class對象

        try {
            Log.d("TAG", "CLASS 類名 " + c.getName());
            Constructor constructor = c.getConstructor(String.class, int.class);//返回類型的構(gòu)造方法
            Log.d("TAG", "構(gòu)造方法的名字 " + constructor.getName());

            Class[] pt = constructor.getParameterTypes();
            for (int i = 0; i < pt.length; i++) {
                Log.d("TAG", "構(gòu)造方法中參數(shù)類型 " + pt[i].getName());
            }

            Field[] fields = c.getFields();//獲取所有的成員變量
            for (Field field : fields) {
                int mod=field.getModifiers();//獲取修飾符
                Log.d("TAG", "獲取所有的成員變量 " + Modifier.toString(mod));

                Class type=field.getType();
                Log.d("TAG", "獲取成員變量名稱" + field.getName());//獲取成員變量名稱
                Log.d("TAG", "獲取成員變量類型 " + type.getName());//獲取成員變量類型

            }

            Method[] methods=c.getMethods();
            for (Method method:methods) {
                Log.d("TAG", "方法名" + method.getName());//方法名
                Log.d("TAG", "方法中參數(shù)的個數(shù)" + method.getParameterCount());//方法中參數(shù)的個數(shù)
                Parameter []parameters=method.getParameters();
                int index=1;
                for (Parameter parameter:parameters){
                    if (parameter.isNamePresent()){
                        Log.d("TAG", "第" + (index++)+"個參數(shù)");
                        Log.d("TAG", " 參數(shù)名稱 " + (parameter.getName()));
                        Log.d("TAG", " 參數(shù)類型 " + (parameter.getType()));
                        Log.d("TAG", " 泛型類型 " + (parameter.getParameterizedType()));

                    }
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

結(jié)論:getFields和getMethods獲取的是修飾符為public的成員變量和方法,還包括了從父類繼承得到的成員變量和方法
getDeclaredFields,etDeclaredMethods獲取的是類中定義的所以成員變量和方法
打印日志:getFields和getMethods


image.png

getDeclaredFields,etDeclaredMethods


image.png

3.equals:是用來判斷兩個對象是否指向了同一塊存儲單元地址
hashcode :倆個對象也可以根據(jù)hashcode來比較
示例代碼:

        String a = "張三";
        String b = new String("張三");
        Log.d("TAG","equals"+a.equals(b));
        Log.d("TAG","=="+(a==b));
        Log.d("TAG","a.hashCode()"+a.hashCode());
        Log.d("TAG","b.hashCode()"+b.hashCode());
        Log.d("TAG","a.hashCode()==b.hashCode()"+(b.hashCode()==a.hashCode()));

打印結(jié)果:


image.png

4.clone()
深克隆是引用類型和值類型都可以復(fù)制
淺克隆是復(fù)制的值類型
5.finalize() 是在垃圾回收時,用于確認該對象是否確認被回收的一個標記,該方法需要重寫才會執(zhí)行,而且只會執(zhí)行一次,對象可以在該方法中自救,避免被垃圾回收掉,同樣自救有只能進行一次(不推薦調(diào)用該方法)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容