Java Object类

Posted Keep

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java Object类相关的知识,希望对你有一定的参考价值。

Object类中的Method有很多,先记几个上课学到的:

public boolean equals(Object o);
//返回调用对象(this)与o对象是否相同
//return o == this
//通常情况是要进行覆盖,判断两个对象是否是同一类型以及封装的值是否相同
public class Student{
    private String sname;
    private int age;
    private String sno;

    public boolean equals(Object o){
        if(null == o) return false;
        if(o.getClass() == this.getClass())
        {
            Student s = (Student)o;
            return this.sname.equals(s.sname) &&
                    this.age == s.age &&
                    this.sno.equals(s.sno)
        }
    }
}

Protected Object clone() throws CloneNotSupportedException

Protected Object clone() throws CloneNotSupportedException
//return a clone of this instrance
//throws CloneNotSupportedException,如果这个类不写implements Cloneable,会抛出这个异常
//这个复制是浅复制,只复制对象的属性值,而对于引用属性,则仅仅是复制引用
//示例
public class Student implements Cloneable{
   public String name;
   public int age;

    public static void main(String[] args) {
        Student s = new Student();
        s.name = "zhangsan";
        s.age = 18;

        try {
           Student s2 = (Student) s.clone();
            System.out.println(s.name);
            System.out.println(s2.name);
        }catch (CloneNotSupportedException r){
        }
    }
}

public String toString()

public String toString()
//returns :a string representation of the object.
//默认为 getClass().getName() + ‘@‘ + Integer.toHexString(hashCode())

protected void finalize() throws Throwable

protected void finalize()
                 throws Throwable
                 //在虚拟机清理垃圾对象之前调用

分割线---------------------------------------------------------------------------------------------------------------

 

以上是关于Java Object类的主要内容,如果未能解决你的问题,请参考以下文章

solr分布式索引实战分片配置读取:工具类configUtil.java,读取配置代码片段,配置实例

java 代码片段

java代码在片段活动中不起作用

Java基础1----Object类解析

15_Java筑基之Object类多态

15_Java筑基之Object类多态