js06金沙官网登录-js06.com-欢迎您

来自 计算机 2019-10-09 16:43 的文章
当前位置: js06金沙官网登录-js06.com-欢迎您 > 计算机 > 正文

金沙官网登录Object类,

Object类,

1 Object类的概述

  • 类Object是类档期的顺序结构的根类。种种类都利用 Object 作为超类。全数目的(满含数组)都实现那些类的诀窍。 
  • Object类是不停抽出而来的。

 

2 equals()方法

public boolean equals(Object obj)

public boolean equals(Object obj) {
        return (this == obj);
    }

 

  • 示例:调用Object类的equals()方法
package java011;

/**
 * 2017/9/18
 * 说明:
 */
public class Person {
    private String name;
    private int age;

    public Person(){

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

}

package java011;

/**
 * 2017/9/18
 * 说明:
 */
public class Test {
    public static void main(String[] args) {
        Person p = new Person("张三",15);
        Person p2 = new Person("张三",15);
        System.out.println(p == p2);//false
        System.out.println(p.equals(p2));//false
    }
}

 

  • 示例:重写equals()方法
package java011;

/**
 * 2017/9/18
 * 说明:
 */
public class Person {
    private String name;
    private int age;

    public Person(){

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


    @Override
    public boolean equals(Object obj) {
        if(obj == null){
            return false;
        }
        if(obj instanceof Person){
            Person p = (Person) obj;
            if(!this.name.equals(p.name) && this.age!=p.age ){
                return false;
            }
        }
        return true;
    }
}

package java011;

/**
 * 2017/9/18
 * 说明:
 */
public class Test {
    public static void main(String[] args) {
        Person p = new Person("张三",15);
        Person p2 = new Person("张三",15);
        System.out.println(p == p2);//false
        System.out.println(p.equals(p2));//true
    }
}

 

3 hashcode()方法

public int hashCode()

public native int hashCode();

 

package java011;

/**
 * 2017/9/18
 * 说明:
 */
public class Person {
    private String name;
    private int age;

    public Person(){

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


    @Override
    public boolean equals(Object obj) {
        if(obj == null){
            return false;
        }
        if(obj instanceof Person){
            Person p = (Person) obj;
            if(!this.name.equals(p.name) && this.age!=p.age ){
                return false;
            }
        }
        return true;
    }
}

package java011;

/**
 * 2017/9/18
 * 说明:
 */
public class Test {
    public static void main(String[] args) {
        Person p = new Person("张三",15);
        Person p2 = new Person("张三",15);
        System.out.println(p);//[email protected]
        System.out.println(p.hashCode());//460141958
        System.out.println(Integer.toHexString(p.hashCode()));//1b6d3586
    }
}

 

4 getClass()方法

public final Class<?> getClass()

public final native Class<?> getClass();

 

  • 示例:
package java011;

/**
 * 2017/9/18
 * 说明:
 */
public class Person {
    private String name;
    private int age;

    public Person(){

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


    @Override
    public boolean equals(Object obj) {
        if(obj == null){
            return false;
        }
        if(obj instanceof Person){
            Person p = (Person) obj;
            if(!this.name.equals(p.name) && this.age!=p.age ){
                return false;
            }
        }
        return true;
    }
}

package java011;

/**
 * 2017/9/18
 * 说明:
 */
public class Test {
    public static void main(String[] args) {
        Person p = new Person("张三",15);
        Class<?> clazz = p.getClass();
        System.out.println(clazz);//class java011.Person
        System.out.println(clazz.getName());//java011.Person
    }
}

 

5 toString()方法

public String toString()

public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }

 

  • 示例:
package java011;

/**
 * 2017/9/18
 * 说明:
 */
public class Person {
    private String name;
    private int age;

    public Person(){

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


    @Override
    public boolean equals(Object obj) {
        if(obj == null){
            return false;
        }
        if(obj instanceof Person){
            Person p = (Person) obj;
            if(!this.name.equals(p.name) && this.age!=p.age ){
                return false;
            }
        }
        return true;
    }
}

package java011;

/**
 * 2017/9/18
 * 说明:
 */
public class Test {
    public static void main(String[] args) {
        Person p = new Person("张三",15);
        System.out.println(p);//[email protected]
        System.out.println(p.toString());//[email protected]
    }
}

 

1 Object类的概述 类Object是类等级次序结构的根类。每一个类都利用 Object 作为超类。全数指标(包括数组)都落到实处那几个类的措施。 Objec...

本文由js06金沙官网登录-js06.com-欢迎您发布于计算机,转载请注明出处:金沙官网登录Object类,

关键词: