Object类的概述
- 类层次结构的根类
(每个类都使用Object类作为超类,所有对象(包括数组)都实现这个方法)
- 所有类都直接或间接的继承自该类
构造方法
- public Object();
- 回想面向对象中为什么说:
子类的构造方法默认访问的是父类中的无参构造方法
Object类的方法
hashCode()方法
- public int hashCode();返回该对象的哈希码值
注意:哈希码值是根据哈希码·算法计算出来的一个值,这个值和地址值有关,但不是实际地址值,你可以理解为地址值
(一般来说,不同的对象哈希码值是不同的)
public class hashCode {
}
public class hashCodetest {
public static void main(String[] args) {
hashCode h1 = new hashCode();
System.out.println(h1.hashCode());
hashCode h2 = new hashCode();
System.out.println(h2.hashCode());
hashCode h3 = h1;
System.out.println(h1.hashCode());
}
}
结果为:
118352462
1550089733
118352462
getClass()方法
- public final class getClass(); 返回此Object类的运行时类
Class类的方法:
public String getName ();以String形式返回此class对象所表示的
public class getClass {
}
public class getClasstest {
public static void main(String[] args) {
getClass g = new getClass();
class c = g.getClass();
String str = c.getName();
System.out.println(str);
//链式编程
String str2 = new g.getClass().getName();
system.out.println(str2);
}
}
网友评论