Object
类是 Java 中所有类的根类,所有类都直接或间接继承自 Object
。它定义了一些通用的方法,以下是 Object
类的常用方法及其含义:
1. toString()
- 作用: 返回对象的字符串表示。
- 默认实现: 返回类名 +
@
+ 对象的哈希码(十六进制)。 - 常用场景: 用于调试和日志记录。
- 示例:
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{name='" + name + "', age=" + age + "}";
}
}
public class Main {
public static void main(String[] args) {
Person person = new Person("Alice", 25);
System.out.println(person.toString()); // 输出: Person{name='Alice', age=25}
}
}
2. equals(Object obj)
- 作用: 判断当前对象与指定对象是否“相等”。
- 默认实现: 比较对象的引用是否相同(即
==
)。 - 常用场景: 重写以实现对象内容的比较。
- 示例:
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Person person = (Person) obj;
return age == person.age && name.equals(person.name);
}
}
public class Main {
public static void main(String[] args) {
Person p1 = new Person("Alice", 25);
Person p2 = new Person("Alice", 25);
System.out.println(p1.equals(p2)); // 输出: true
}
}
3. hashCode()
- 作用: 返回对象的哈希码值。
- 默认实现: 返回对象的内存地址的哈希码。
- 常用场景: 在哈希表(如
HashMap
、HashSet
)中使用。 - 规则:
- 如果两个对象相等(
equals
返回true
),则它们的哈希码必须相等。 - 如果两个对象不相等,哈希码不一定不同。
- 如果两个对象相等(
- 示例:
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
4. getClass()
- 作用: 返回对象的运行时类(
Class
对象)。 - 常用场景: 反射、类型检查。
- 示例:
public class Main {
public static void main(String[] args) {
Person person = new Person("Alice", 25);
System.out.println(person.getClass()); // 输出: class Person
}
}
5. clone()
- 作用: 创建并返回当前对象的副本。
- 注意:
- 需要实现
Cloneable
接口,否则会抛出CloneNotSupportedException
。 - 默认是浅拷贝。
- 需要实现
- 示例:
public class Person implements Cloneable {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class Main {
public static void main(String[] args) throws CloneNotSupportedException {
Person p1 = new Person("Alice", 25);
Person p2 = (Person) p1.clone();
System.out.println(p1.equals(p2)); // 输出: true
}
}
6. finalize()
- 作用: 当垃圾回收器确定对象不再被引用时调用。
- 注意:
- 不推荐使用,因为垃圾回收的时间不确定。
- Java 9 后已弃用。
- 示例:
public class Person {
@Override
protected void finalize() throws Throwable {
System.out.println("Person对象被回收");
}
}
7. wait()
、notify()
、notifyAll()
- 作用: 用于线程间的通信和同步。
- 注意:
- 必须在同步代码块或同步方法中调用。
wait()
: 使当前线程等待,直到其他线程调用notify()
或notifyAll()
。notify()
: 唤醒一个等待的线程。notifyAll()
: 唤醒所有等待的线程。
- 示例:
public class Main {
public static void main(String[] args) throws InterruptedException {
Object lock = new Object();
Thread t1 = new Thread(() -> {
synchronized (lock) {
try {
lock.wait(); // 等待
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread 1 被唤醒");
}
});
Thread t2 = new Thread(() -> {
synchronized (lock) {
lock.notify(); // 唤醒
System.out.println("Thread 2 唤醒 Thread 1");
}
});
t1.start();
Thread.sleep(1000);
t2.start();
}
}
总结
方法 | 作用 |
---|---|
toString() | 返回对象的字符串表示。 |
equals() | 判断对象是否相等。 |
hashCode() | 返回对象的哈希码。 |
getClass() | 返回对象的运行时类。 |
clone() | 创建并返回对象的副本。 |
finalize() | 垃圾回收时调用(已弃用)。 |
wait() | 使当前线程等待。 |
notify() | 唤醒一个等待的线程。 |
notifyAll() | 唤醒所有等待的线程。 |
这些方法是 Java 编程的基础,理解它们的用途和实现方式对编写高质量的代码非常重要。
THE END
暂无评论内容