Some methods hashCode java Object class () getClass () toString ()

Object(类)
public int hashCode()
public final Class getClass()
public String toString()
public boolean equals(Object obj)

11.17_ common objects (API overview as well as an overview of the Object class) (master)

A:API(Application Programming Interface) 
	应用程序编程接口
B:Java API
	就是Java提供给我们使用的类,这些类将底层的实现封装了起来,
	我们不需要关心这些类是如何实现的,只需要学习这些类如何使用。
C:Object类概述
	类层次结构的根类
	所有类都直接或者间接的继承自该类
D:构造方法
	public Object()
	回想面向对象中为什么说:
	子类的构造方法默认访问的是父类的无参构造方法

11.18_ common objects (hashCode Object class () method) (master)

A:案例演示
	public int hashCode()
		a:返回该对象的哈希码值。默认情况下,该方法会根据对象的地址来计算。
		b:不同对象的,hashCode()一般来说不会相同。
	  	   但是,同一个对象的hashCode()值肯定相同。
		c:不是对象的实际地址值,可以理解为逻辑地址值。	

11.19_ common objects (getClass Object class () method) (master)

A:案例演示
	public final Class getClass()
		a:返回此 Object 的运行时类。
		b:可以通过Class类中的一个方法,获取对象的真实类的全名称。	
		public String getName()

11.20_ Common Object (toString Object class () method) (master)

A:案例演示
	public String toString()
	a:返回该对象的字符串表示。
		源代码:
		 	public String toString() {
   					 return getClass().getName() + "@" + Integer.toHexString(hashCode());
				}
	b:它的值等于: 
		getClass().getName() + '@' + Integer.toHexString(hashCode()) 
	c:由于默认情况下的数据对我们来说没有意义,一般建议重写该方法。
	  怎么重写, 一般是将该类的所有的成员变量组成返回即可
B:最终版
	自动生成
C: 直接输出对应的名称,就是调用对象的toString()方法

11.21_ common objects (equals Object class () method) (master)

A:案例演示
	a:指示其他某个对象是否与此对象“相等”。 
		源代码: 	
				public boolean equals(Object obj) {
    					return (this == obj);
				}
	b:默认情况下比较的是对象的引用是否相同。
	c:由于比较对象的引用没有意义,一般建议重写该方法。一般用于比较成员变量的值是否相等
	d:==和equals()的区别。(面试题)

11.22_ common objects (equals Object class () method code optimization) (master)

A:案例演示
	Object类的equals()方法代码优化
		a: 提高效率
		b: 提高健壮性(instanceof)	
B:最终版
	自动生成

11.23_ common objects (clone Object class () method) (master)

clone () modifier rights are protected, when in use, so that the class override this method, and the authority of the method was changed to public modifier
object shallow clone: Clone shallow and deep clone
using the clone () method uses a shallow clone manner

对象浅克隆要注意的细节: 
  1. If an object needs to call clone cloning method, then the object belongs to the class must implement the Cloneable interface.

  2. Cloneable interface is just a marker interface only, there is no way.

  3. Shallow clone is a clone of the object when the object if the cloned object maintained in another class, this time just clone another object of address, but not the
    other objects are also a clone.

  4. Shallow clone will not call the constructor.

    Deep clone (hereinafter speaking): IO stream using ObjectOutputStream implemented using object written to the file, and then read back ObjectInputStream

public class MyTest {
	 public static void main(String[] args) throws CloneNotSupportedException {
		 Person p1 = new Person("狗娃", 23);
		 Person p2 = (Person) p1.clone();
		        p2.name="张三";
		        p2.age=30;
			System.out.println(p1);
			System.out.println(p2);
	System.out.println("---------------------------");
		 Address address = new Address("北京");
		Person p3 = new Person("王五", 25, address);
		Person p4 = (Person) p3.clone();
		p4.name="赵六";
		p4.age=36;
		p4.address.city="上海";
		System.out.println(p3);
		System.out.println(p4);
}

}
class Person implements Cloneable{
public String name;
public int age;
public Address address;
public Person(String name, int age) {
this.name = name;
this.age = age;
}

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

@Override
public Object clone() throws CloneNotSupportedException {
    return super.clone();
}

@Override
public String toString() {
    return "Person{" +
            "name='" + name + '\'' +
            ", age=" + age +
            ", address=" + address +
            '}';
}

}
class Address{
public String city;

public Address(String city) {
    this.city = city;
}

@Override
public String toString() {
    return "Address{" +
            "city='" + city + '\'' +
            '}';
}

}

Guess you like

Origin blog.csdn.net/haha9417/article/details/93772470