您的位置:首页 > 移动开发 > Objective-C

java笔记--day11--类object之hashCode() and getClass()

2016-09-17 20:04 561 查看
1 Object类的概述

类 Object 是类层次结构的根类。每个类都使用 Object 作为超类。所有对象(包括数组)都实现这个类的方法。

Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.

2 成员方法

public int hashCode()

public final Class getClass()

public String toString()

public boolean equals(Object obj)

protected void finalize()

protected Object clone()

本小节讲解方法hashCode()和getClass()

3 public int hashCode()

暂时当作对象的地址

4 public final Class getClass()

可以看出,返回此 Object 的运行时类。

这里的Class是一个类,该类中有一个public string getName()方法

该类以 String 的形式返回此 Class 对象所表示的实体(类、接口、数组类、基本类型或 void)名称。

5 代码实现:

public class StudentDemo {
public static void main(String[] args) {
Student s1 = new Student();
System.out.println(s1.hashCode());// 57410271
Student s2 = new Student();
System.out.println(s2.hashCode());// 674267194
Student s3 = s1;
System.out.println(s3.hashCode());// 57410271

Student s = new Student();
System.out.println(s.getClass());// class xsh_itcast_02.Student
System.out.println(s.getClass().getName());// xsh_itcast_02.Student
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: