Object类
来源:互联网 发布:jdk 7u79 windows x86 编辑:程序博客网 时间:2024/06/14 03:41
Object:所有类的根类。
Object类是不断抽取而来的,具备着所有对象都具备的共性内容。
Object类中的常见方法:
1.equals():方法,判断其他对象是否与此对象相等(当且仅当两个变量引用的是同一个对象时,才会返回TRUE)。
class Person{ private int age; Person(int age){ this.age = age; }}class ObjectDemo{ public static void main(String[]args){ Person p1 = new Person(20); Person p2 = new Person(20); Person p3 = p1; Demo d = new Demo(); System.out.println(p1==p2); System.out.println(p1.equals(p3)); }}//引用同一个对象才会返回trueclass Person{ private int age; Person(int age){ this.age = age; }}class ObjectDemo{ public static void main(String[]args){ Person p1 = new Person(20); Person p2 = new Person(20); Demo d = new Demo(); System.out.println(p1==p2); System.out.println(p1.equals(p2)); }}//即使内容完全一样,不是同一个对象,返回也是FALSE
一般都会覆盖此方法,根据对象的特有内容,建立判断对象是否相等的一句。
2.hashCode():直接输出对象,类名后边的就是哈希值(十进制转换十六进制方法Integer.toHexString())
class Person{ private int age; Person(int age){ this.age = age; }}class ObjectDemo{ public static void main(String[]args){ Person p1 = new Person(20); Person p2 = new Person(20); System.out.println(p1); }}//可以进行哈希码重写class Person{ private int age; Person(int age){ this.age = age; } public int hashCode(){ return age; }}class ObjectDemo{ public static void main(String[]args){ Person p1 = new Person(20); Person p2 = new Person(20); System.out.println(Integer.toHexString(p1.hashCode())); }}
3.getClass():返回此object的运行时类(当前对象所属的字节码文件对象)
class Person{ private int age; Person(int age){ this.age = age; } public int hashCode(){ return age; }}class ObjectDemo{ public static void main(String[]args){ Person p1 = new Person(20); Person p2 = new Person(20); Class clazz1 = p1.getClass(); Class clazz2 = p2.getClass(); System.out.println(clazz1==clazz2); }}
4.toString():将对象转换成字符串的表现形式
任何一个对象,只要你想创建起特定的字符串表现形式,就直接将toString方法覆盖掉就可以
0 0
- Object类
- Object类
- Object类
- Object类
- Object类
- Object类
- Object类
- Object类
- Object类
- object类
- Object类
- Object类
- Object类
- Object类
- Object类
- Object类
- Object类
- Object类
- Netty Failure to transfer io.netty:netty-tcnative:jar:${os.detected.classifier}:2.0.0.Final 问题解决
- 湖北民族学院oj 1883 之 Sequence Number
- as中drawable和mipmap都用右键res,new image asset方法放入
- Angular4中的依赖注入
- 程序员在十年后还会有今天的收入吗?
- Object类
- poj 1936 求一个串 是否为 另一个的自串(可以不连续)
- 百炼4116:拯救行动
- Minimum Size Subarray Sum
- 快速学习四步法:如何用20小时,快速学习?
- QT QTableView 获取单击选中行的内容
- C++之goto
- CCF NOI1149 N皇后问题
- LeetCode dynamic programming 72. Edit Distance