Object
类是 Java 中所有类的父类,每个类都直接或间接地继承自 Object
类。Object
类中定义了一些常用的方法,这些方法在 Java 编程中非常有用。以下是 Object
类中的一些常见方法及其用途:
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 + "}";
}
}
Person person = new Person("Alice", 30);
System.out.println(person.toString()); // 输出: Person{name='Alice', age=30}
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);
}
}
Person person1 = new Person("Alice", 30);
Person person2 = new Person("Alice", 30);
System.out.println(person1.equals(person2)); // 输出: true
hashCode()
HashMap
)中的快速查找。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() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + age;
return result;
}
}
Person person = new Person("Alice", 30);
System.out.println(person.hashCode()); // 输出: 哈希码值
clone()
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();
}
}
Person person1 = new Person("Alice", 30);
try {
Person person2 = (Person) person1.clone();
System.out.println(person2); // 输出: Person@<hashcode>
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
finalize()
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
protected void finalize() throws Throwable {
System.out.println("Finalizing " + name);
super.finalize();
}
}
Person person = new Person("Alice", 30);
person = null;
System.gc(); // 请求垃圾回收
getClass()
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
Person person = new Person("Alice", 30);
System.out.println(person.getClass().getName()); // 输出: Person
wait()
, notify()
, notifyAll()
wait()
使当前线程等待,notify()
唤醒一个等待的线程,notifyAll()
唤醒所有等待的线程。public class Counter {
private int count = 0;
public synchronized void increment() {
count++;
notifyAll();
}
public synchronized void decrement() {
while (count == 0) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
count--;
}
public synchronized int getCount() {
return count;
}
}
Counter counter = new Counter();
Thread t1 = new Thread(() -> {
for (int i = 0; i < 10; i++) {
counter.increment();
System.out.println("Incremented to " + counter.getCount());
}
});
Thread t2 = new Thread(() -> {
for (int i = 0; i < 10; i++) {
counter.decrement();
System.out.println("Decremented to " + counter.getCount());
}
});
t1.start();
t2.start();
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。