后端---java中object类中有几个方法

首先我们要知道JAVA是一个与C++多根继承机制语言而不同只允许单根继承的语言,并且我们在JAVA中定义的所有类都有一个共同的祖先类Object类 

下面我们从java API中一探究竟

Method Summary
protected  Objectclone() 
          Creates and returns a copy of this object.
 booleanequals(Object obj) 
          Indicates whether some other object is "equal to" this one.
protected  voidfinalize() 
          Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
 Class<?>getClass() 
          Returns the runtime class of this Object.
 inthashCode() 
          Returns a hash code value for the object.
 voidnotify() 
          Wakes up a single thread that is waiting on this object's monitor.
 voidnotifyAll() 
          Wakes up all threads that are waiting on this object's monitor.
 StringtoString() 
          Returns a string representation of the object.
 voidwait() 
          Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.
 voidwait(long timeout) 
          Causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.
 voidwait(long timeout, int nanos) 
          Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.

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 

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

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

 

finalize() 

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

这个函数在进行垃圾回收的时候会用到,匿名对象回收之前会调用到.

对于所有类都有一个祖先Object类的解释:

如果A 没有继承任何类,那他的类层次关系默认是 A -- Object.
如果A 继承了类B,那他的类层次关系变为 A -- B -- Object,Object是他爷爷,B是他唯一的父亲,B直接继承Objcet,A间接继承Object.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值