Object类

# Object类

API就是Application Programming Interfaces,就是应用程序接口

## 常用方法

– protected Object clone() 创建并返回一个对象的拷贝
– boolean equals(Object obj) 比较两个对象是否相等
– String toString() 返回对象的字符串表示形式
– Class getClass() 获取对象的运行时对象的类
– int hashCode() 获取对象的 hash 值
– protected void finalize() 当 GC (垃圾回收器)确定不存在对该对象的有更多引用时,由对象的垃圾回收器调用此方法。
– void notify() 唤醒在该对象上等待的某个线程
– void notifyAll() 唤醒在该对象上等待的所有线程
– void wait() 让当前线程进入等待状态。
直到其他线程调用此对象的 notify() 方法或 notifyAll() 方法。
– void wait(long timeout) 让当前线程处于等待(阻塞)状态,直到其他线程调用此对象的 notify() 方法
或 notifyAll() 方法,或者超过参数设置的timeout超时时间。
– void wait(long timeout, int nanos)与 wait(long timeout) 方法类似,多了一个 nanos 参数。
这个参数表示额外时间(以纳秒为单位,范围是0-999999)。 所以超时的时间还需要加上 nanos 纳秒。。`

“`java
public class TestDemo implements Cloneable {

int i;

public static void main(String[] args) throws CloneNotSupportedException {
TestDemo testDemo = new TestDemo();
testDemo.i = 10;
System.out.println(testDemo);
// 克隆对象。克隆出的对象和原来的对象属性值一样
// CloneNotSupportedException:任何一个对象想要被克隆,它的类必须实现Cloneable接口
Object clone = testDemo.clone();
TestDemo testDemo1 = (TestDemo) clone;
System.out.println(testDemo1.i);
System.out.println(testDemo1);

Object obj = new Object();
// 过时的方法
// 通过GC回收对象,但是GC不一定执行
// obj.finalize();
// System.gc();

// 获取对象运行时的真正类型
Object obj1 = “abc”;
System.out.println(obj1.getClass());
// 获取对象的哈希码值
// 哈希码就是根据哈希散列算法计算出来的一个值
// 这个值会随机散落在将近43亿个值上
// 人为的认为同一个类的不同对象的哈希码值是唯一的
// 往往根据哈希码来决定对象的内存存储
System.out.println(obj.hashCode());

// toString() 把对象转换成字符串
String str = obj.toString();
System.out.println(obj);
System.out.println(str);

Person person = new Person();
person.setName(“令狐冲”);
person.setAge(20);
person.setAddress(“华山派”);
System.out.println(person);

// 对于对象来说 == 就是判断地址是否相同
boolean equals = person.equals(obj);
System.out.println(equals);

}
}
“`

### 重写equals

“`java
// 对象的属性值都一样,就是同一个对象
@Override
public boolean equals(Object obj) {
// 判断内存地址是否一致
if (this == obj) {
return true;
}
// 判断obj是否是null,防止空指针异常
if (obj == null) {
return false;
}

// 比较类型是否相同
if (this.getClass() != obj.getClass()) {
return false;
}
// 比较属性值
// 强转
Person p = (Person) obj;

// 比较年龄是否相同
if (this.age != p.age) {
return false;
}

// 比较姓名是否相同
if (this.name == null) {
if (p.name != null) {
return false;
}
} else if (!this.name.equals(p.name)) {
return false;
}

// 比较住址是否相同
if (this.address == null) {
if (p.address != null) {
return false;
}
} else if (!this.address.equals(p.address)) {
return false;
}
return true;
}
“`

“`java
@Override
public boolean equals(Object o) {
// 判断内存地址是否相同
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return age == user.age && Objects.equals(name, user.name) && Objects.equals(address, user.address);
}
“`

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
下一篇