zoukankan      html  css  js  c++  java
  • 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
                     //在虚拟机清理垃圾对象之前调用

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

    你若笃定,世界便不浮躁。
  • 相关阅读:
    cookie的设置、获取和删除封装
    原生javascript封装ajax和jsonp
    javascript模块化应用
    图解javascript this指向什么?
    学习bootstrap心得
    javascript使用两个逻辑非运算符(!!)的原因
    dubbo小教程
    JSTL与EL表达式(为空判断)
    自己整理的常用SQL Server 2005 语句、
    python基础:迭代器、生成器(yield)详细解读
  • 原文地址:https://www.cnblogs.com/zhangyue123/p/9277378.html
Copyright © 2011-2022 走看看