Object类的方法

本文详细介绍了Java语言中Object类的功能及重要方法,包括构造函数、hashCode和equals方法、多线程相关的wait和notify方法等,并解释了这些方法如何在实际应用中发挥作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

Java语言不同于C++语言,是一种单根继承结构语言,也就是说,Java中所有的类都有一个共同的祖先。这个祖先就是Object类。

object类的结构

Object类的函数 
如图可知,Object类有12个成员方法,按照用途可以分为以下几种 
1,构造函数 
2,hashCode和equale函数用来判断对象是否相同, 
3,wait(),wait(long),wait(long,int),notify(),notifyAll() 
4,toString()和getClass, 
5,clone() 
6,finalize()用于在垃圾回收

函数说明

clone()

clone()函数的用途是用来另存一个当前存在的对象。

hashCode()和equale()

  • equale()用于确认两个对象是否相同。
  • hashCode()用于获取对象的哈希值,这个值的作用是检索,具体的作用可以参考这里
  • 哈希值相同的对象不一定equale()
  • equale()返回true的两个对象一定相同。

toString()和getClass()

toString()返回一个String对象,用来标识自己 
getClass()返回一个Class对象,如果打印出来会发现结果是如下格式

class package.name.xxx 
 
  • 1

因为返回的是一个class对象,后面可以跟class类的方法。用的是谁的构造函数,那么getClass返回的就是谁的类型。 
getClass()经常用于java反射机制

wait(),wait(long),wait(long,int),notify(),notifyAll()

  • 这几个函数体现的是Java的多线程机制
  • 在使用的时候要求在synchronize语句中使用
  • wait()用于让当前线程失去操作权限,当前线程进入等待序列
  • notify()用于随机通知一个持有对象的锁的线程获取操作权限
  • notifyAll()用于通知所有持有对象的锁的线程获取操作权限
  • wait(long) 和wait(long,int)用于设定下一次获取锁的距离当前释放锁的时间间隔

finalize()

这个函数在进行垃圾回收的时候会用到,匿名对象回收之前会调用到,具体的例子如图所示

finalize函数说明


原文链接:http://blog.youkuaiyun.com/u013894427/article/details/53468038

Rebuild started: Project: Project *** Using Compiler 'V6.22', folder: 'E:\Keil_v5\ARM\ARMCLANG\Bin' Rebuild target 'Target 1' assembling startup_stm32f10x_md.s... Start/core_cm3.c(445): error: non-ASM statement in naked function is not supported 445 | uint32_t result=0; | ^ Start/core_cm3.c(442): note: attribute is here 442 | uint32_t __get_PSP(void) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(465): error: parameter references not allowed in naked functions 465 | "BX lr \n\t" : : "r" (topOfProcStack) ); | ^ Start/core_cm3.c(461): note: attribute is here 461 | void __set_PSP(uint32_t topOfProcStack) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(479): error: non-ASM statement in naked function is not supported 479 | uint32_t result=0; | ^ Start/core_cm3.c(476): note: attribute is here 476 | uint32_t __get_MSP(void) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(499): error: parameter references not allowed in naked functions 499 | "BX lr \n\t" : : "r" (topOfMainStack) ); | ^ Start/core_cm3.c(495): note: attribute is here 495 | void __set_MSP(uint32_t topOfMainStack) __attribute__( ( naked ) ); | ^ 4 errors generated. compiling core_cm3.c... compiling misc.c... compiling system_stm32f10x.c... compiling stm32f10x_adc.c... compiling stm32f10x_dac.c... compiling stm32f10x_exti.c... compiling stm32f10x_dbgmcu.c... compiling stm32f10x_dma.c... compiling stm32f10x_crc.c... compiling stm32f10x_cec.c... compiling stm32f10x_bkp.c... compiling stm32f10x_can.c... compiling stm32f10x_flash.c... compiling stm32f10x_pwr.c... compiling stm32f10x_fsmc.c... compiling stm32f10x_
03-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值