码迷,mamicode.com
首页 > 编程语言 > 详细

Java Object类

时间:2018-07-07 17:50:57      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:tran   private   equal   zha   repr   bool   java   imp   exception   

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类

标签:tran   private   equal   zha   repr   bool   java   imp   exception   

原文地址:https://www.cnblogs.com/zhangyue123/p/9277378.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!