object类

1.equals方法

public boolean equals(Object obj):Object 对象调用该方法用于比较同一个类的两个不同的对象是否具有相同的内容.

     注意:“==”运算符比较的是两个对象引用是否指向同一对象,即判断它在内存中是否在同一区域。

例:public class Equals {
                       public static void main(String[] args)
                       {
                           String str1=new String ("One world,One Dream");
                           String str2=new String ("One world,One Dream");
                           String str3="One world,One Dream";
                           String str4="One world,One Dream";
                           System.out.println("str1.equals(str2):"+str1.equals(str2));
                           System.out.println("str1==str2:"+(str1==str2));
                           System.out.println("str1.equals(str3):"+str1.equals(str3));
                           System.out.println("str1==str3:"+(str1==str3));
                           System.out.println("str3.equals(str4):"+str3.equals(str4));
                           System.out.println("str3==str4:"+(str3==str4));
                       }
}
2.toString 用法

Public String toString():Object对象调用该方法将返回一个代表有关当前对象本身信息的字符串。默认情况下,返回的字符串由该对象所属的类名,”@“符号和代表该对象的无符号十六进制数(一般是对象的内存地址)组成。

       Object 类中的toString 方法返回的默认字符串在使用上一般没有什么价值,应使用子类重写该个方法,使它返回一个易懂易读的字符串。

package yy;
class Teacher
{
    String Tname;
    Teacher(String name)
    {
        Tname=name;
    }
}
class Student
{
    String Sname;
    Student(String name)
    {
        Sname=name;
    }
    public String toString()
    {
        return Sname;
    }
}

public class ToString {
            public static void main(String[] args)
            {
                Teacher li=new Teacher("李老师");
                Student zhang=new Student("张三");
                System.out.println(li.toString());
                System.out.println(arg0);
            }
}

3.clone方法

protected Object clone () throws CloneNotSupportedException :创建一个对象并将该对象复制到一个新的内存地址,同时引用指向新对象。

注意:在Object类中,clone方法被声明为projected,这说明不能在其他包的类中访问不属于同一类对象的clone方法。所以,如果打算让其他类也能复制某个类的对象,就需要在该类中重写clone方法。并将其声明为public的。

同时Java语言出于安全等方面的考虑,在默认情况下,定义的类都不具有克隆能力。

为了使一个类的对象能够成功克隆,该类还应实现Cloneable接口。Cloneable接口的定义如下:

public interface Cloneable {}

 该接口是空的,称为标记接口(market interface)。Java语言规定,要使一个类的对象可以克隆,该类必须实现Cloneable接口。否则,调用clone()方法时,将会抛出CloneNotSupportedException异常。

package yy;
class CloneClass implements Cloneable
{
    int i;
    public CloneClass (int x)
    {
        i=x;
    }
    public Object clone()
    {
        Object object=null;
        try
        {
            object=super.clone();
            
        }
        catch (CloneNotSupportedException e)
        {
            System.out.println("克隆失败");
            
        }
        return object;
    }
    public boolean equals(Object obj)
    {
        if(obj instanceof CloneClass)
            return ((CloneClass)obj).i==i;
        return false;
    }
    public String toString()
    {
        return ""+i;
        
    }
}

public class Clone {
           public static void main(String args[])
           {
               CloneClass c1=new CloneClass(100);
               CloneClass c2=(CloneClass)c1.clone();
               System.out.println("c1克隆出了c2");
               if(c1==c2)
               
                   System.out.println("c1与c2指向同一个地址单元");
                   else
                       System.out.println("c1与c2直向不同的地址单元");
                   if(c1.equals(c2))
System.out.println("c1与c2内容相同");
                   else 
                       System.out.println("c1与c2内容不相同");
                   System.out.println("c1.toString()="+c1.toString());
                   System.out.println("c1.toString()="+c2.toString());
               
           }
}
4.getclass方法

public final Class getClass():Object对象调用该方法返回Object的运行类。

JVM在将一个类的对象装载进来时,都会产生一个class对象。该对象包含了所装载类的元信息,如类的名字,属性,方法 ,构造函数和父类等信息。

package yy;
class People 
{
    String name;
    int age;
    People(String str,int i)
    {
        name=str;
        age=i;
    }
    public void Introduce()
    {
        System.out.println(name+age);
    }
}
public class Getclass {
             public static void main(String[] args)
             {
                 People boy=new People("张三",28);
                 System.out.println("boy所属的类:"+boy.getClass());
         
             }
}
 

原文地址:https://www.cnblogs.com/chmusk/p/11078958.html