java核心技术----Object类
2024-10-08 06:45:07
package java.lang; /**
* Class {@code Object} is the root of the class hierarchy.
* Every class has {@code Object} as a superclass. All objects,
* including arrays, implement the methods of this class.
*
* @author unascribed
* @see java.lang.Class
* @since JDK1.0
*/
public class Object { private static native void registerNatives();
static {
registerNatives();
} public final native Class<?> getClass(); public native int hashCode(); public boolean equals(Object obj) {
return (this == obj);
} protected native Object clone() throws CloneNotSupportedException; public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
} public final native void notify(); public final native void notifyAll(); public final native void wait(long timeout) throws InterruptedException; public final void wait(long timeout, int nanos) throws InterruptedException {
if (timeout < 0) {
throw new IllegalArgumentException("timeout value is negative");
} if (nanos < 0 || nanos > 999999) {
throw new IllegalArgumentException(
"nanosecond timeout value out of range");
} if (nanos > 0) {
timeout++;
} wait(timeout);
} public final void wait() throws InterruptedException {
wait(0);
} protected void finalize() throws Throwable { }
}
Object类是Java中所有类的始祖,在Java中每个类都是由它扩展而来。
在Java中,只有基本类型不是对象(数组也都扩展了Object类)。
/**
* Created by N3verL4nd on 2016/12/4.
* Object类:所有类的超类
* Object类是不断抽取而来,具备着所有对象都具备的共性内容。
* 常用的共性内容:
* equals toString hashCode clone
*/ class Person{
private int age; public Person(int age){
this.age = age;
}
} public class test {
public static void main(String... args) {
Person p1 = new Person(20);
Person p2 = new Person(20);
System.out.println(p1 == p2);
System.out.println(p1.equals(p2));
}
}
输出:
false
false
equals比较的是两个对象是否指向同一个位置
重写equals方法:
/**
* Created by N3verL4nd on 2016/12/4.
* Object类:所有类的超类
* Object类似不断抽取而来,具备着所有对象都具备的共性内容。
* 常用的共性内容:
* equals toString hashCode clone
*/ class Person{
private int age; public Person(int age){
this.age = age;
} //根据Person类的年龄进行比较
@Override
public boolean equals(Object obj){
if (!(obj instanceof Person)) {
throw new ClassCastException("类型错误");
}
Person p = (Person)obj;
return (age == p.age);
}
} public class test {
public static void main(String... args) {
Person p1 = new Person(20);
Person p2 = new Person(21);
Person p3 = new Person(20);
System.out.println(p1 == p2);
System.out.println(p1.equals(p2));
System.out.println(p1.equals(p3));
}
}
输出:
false
false
true
/**
* Created by N3verL4nd on 2016/12/4.
* Object类:所有类的超类
* Object类似不断抽取而来,具备着所有对象都具备的共性内容。
* 常用的共性内容:
* equals toString hashCode clone
*/ class Person{
private int age; public Person(int age){
this.age = age;
} //根据Person类的年龄进行比较
@Override
public boolean equals(Object obj){
if (!(obj instanceof Person)) {
throw new ClassCastException("类型错误");
}
Person p = (Person)obj;
return (age == p.age);
}
} public class test {
public static void main(String... args) {
Person p1 = new Person(20);
Person p2 = new Person(21);
Person p3 = new Person(20);
Demo demo = new Demo();
System.out.println(p1 == p2);
System.out.println(p1.equals(p2));
System.out.println(p1.equals(p3));
System.out.println("p1 = " + p1);
System.out.println("p1.hashCode() = " + p1.hashCode());
System.out.println("Integer.toHexString(p1.hashCode()) = " + Integer.toHexString(p1.hashCode()));
}
}
输出:
false false true p1 = Person@28d93b30 p1.hashCode() = 685325104 Integer.toHexString(p1.hashCode()) = 28d93b30 |
一般重写equals方法后都需要重写HashCode,因为相等的两个对象必须确保hashCode相等。
/**
* Created by N3verL4nd on 2016/12/4.
* Object类:所有类的超类
* Object类似不断抽取而来,具备着所有对象都具备的共性内容。
* 常用的共性内容:
* equals toString hashCode clone
*/ class Person{
private int age; public Person(int age){
this.age = age;
} //根据Person类的年龄进行比较
@Override
public boolean equals(Object obj){
if (!(obj instanceof Person)) {
throw new ClassCastException("类型错误");
}
Person p = (Person)obj;
return (age == p.age);
} @Override
public int hashCode(){
return age;
}
} public class test {
public static void main(String... args) {
Person p1 = new Person(20);
Person p2 = new Person(21);
Person p3 = new Person(20);
Demo demo = new Demo();
System.out.println(p1 == p2);
System.out.println(p1.equals(p2));
System.out.println(p1.equals(p3));
System.out.println("p1 = " + p1);
System.out.println("p1.hashCode() = " + p1.hashCode());
System.out.println("Integer.toHexString(p1.hashCode()) = " + Integer.toHexString(p1.hashCode()));
}
}
/**
* Created by N3verL4nd on 2016/12/4.
* Object类:所有类的超类
* Object类似不断抽取而来,具备着所有对象都具备的共性内容。
* 常用的共性内容:
* equals toString hashCode clone
*/ class Person{
private int age; public Person(int age){
this.age = age;
} //根据Person类的年龄进行比较
@Override
public boolean equals(Object obj){
if (!(obj instanceof Person)) {
throw new ClassCastException("类型错误");
}
Person p = (Person)obj;
return (age == p.age);
} /*
@Override
public int hashCode(){
return age;
}
*/
} public class test {
public static void main(String... args) {
Person p1 = new Person(20);
System.out.println(p1);
System.out.println(p1.getClass().getName() + "@" + Integer.toHexString(p1.hashCode()));
}
}
重写toString方法:
/**
* Created by N3verL4nd on 2016/12/4.
* Object类:所有类的超类
* Object类似不断抽取而来,具备着所有对象都具备的共性内容。
* 常用的共性内容:
* equals toString hashCode clone
*/ class Person{
private int age; public Person(int age){
this.age = age;
} //根据Person类的年龄进行比较
@Override
public boolean equals(Object obj){
if (!(obj instanceof Person)) {
throw new ClassCastException("类型错误");
}
Person p = (Person)obj;
return (age == p.age);
} /*
@Override
public int hashCode(){
return age;
}
*/ public String toString(){
return "Person@@" + age;
}
} public class test {
public static void main(String... args) {
Person p1 = new Person(20);
System.out.println(p1);
System.out.println(p1.getClass().getName() + "@" + Integer.toHexString(p1.hashCode()));
}
}
输出:
Person@@20
Person@28d93b30
最新文章
- Rss 订阅:php动态生成xml格式的rss文件
- SqlServer英文单词全字匹配
- JavaScript Patterns 6.5 Inheritance by Copying Properties
- WD硬盘型号信息
- @synthesize vs. @dynamic
- LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]
- Linux ubuntu 10.10安装OpenCv
- Effective C++ 的55个条款
- MongoDB 快速入门--初级
- windows log
- mysql主从之主键冲突
- Cracking the coding interview
- C# Trim方法去除字符串两端的指定字符
- mysql记录所有执行过的SQL
- Jsonql——给RESTful API插上一对翅膀
- 团队作业8——第二次项目冲刺(Beta阶段)Day2--5.19
- UVa439——骑士的移动
- TIJ -- CountDownLatch
- CentOS7安装Jdk1.8
- [UE4]响应鼠标点击
热门文章
- selenium自动化之xpath定位*必会技能*
- VMware 完成 27 亿美元的 Pivotal 收购 | 云原生生态周报 Vol. 34
- schedule of 2016-11-7~2016-11-10(Monday~Thursday)——1st semester of 2nd Grade
- 比特币学习笔记(一)---在windows下编译搭建比特币环境
- 关于Mach-O类型文件那点事
- Netty快速入门(08)ByteBuf组件介绍
- 快速回顾MySQL:简单查询操作
- .NET Core 3 WPF MVVM框架 Prism系列之事件聚合器
- vPlayer 模块Demo
- cogs 1176. [郑州101中学] 月考 Map做法