鱼C论坛

 找回密码
 立即注册
查看: 2991|回复: 0

[学习笔记] 0基础学习Java ——Object 对象(下)

[复制链接]
发表于 2017-8-16 09:47:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 回忆一遥远 于 2017-8-16 09:51 编辑

Object 对象



Java 中所有的类都直接或间接继承 Object ,因此都有如下方法

getClass();
hashCode();
equals(Object obj);
clone();
toString();
notify();
notifyAll();
wait();
finalize();




clone

protected Object clone()
                throws CloneNotSupportedException

创建并返回这个对象的一个副本。(默认浅拷贝)
  1.     /**
  2.      * @return     a clone of this instance.
  3.      * @throws  CloneNotSupportedException  if the object's class does not
  4.      *               support the {@code Cloneable} interface. Subclasses
  5.      *               that override the {@code clone} method can also
  6.      *               throw this exception to indicate that an instance cannot
  7.      *               be cloned.
  8.      * @see java.lang.Cloneable
  9.      */
  10.     protected native Object clone() throws CloneNotSupportedException;
复制代码


例如:
  1. class Test1 implements Cloneable{
  2.         int num;
  3.         String s = "初始值";
  4.         Test1(int temp){
  5.                 num = temp;
  6.                 System.out.println("num = " + num);
  7.         }

  8.         Test1(String temp){
  9.                 System.out.println("s = " + temp);
  10.                 s = temp;
  11.         }

  12.         Test1(String s, int n){
  13.                 this(n);
  14.                 // 构造器中调用构造器!
  15.                 this.s = s;
  16.                 System.out.println("String and int args");
  17.         }

  18.         void Pain(){
  19.                 System.out.println("num = " + num + " s = " + s);
  20.         }
  21.        
  22.         @Override
  23.     protected Object clone() throws CloneNotSupportedException {
  24.         return super.clone();
  25.     }
  26. }

  27. public class Test {
  28.         public static void main(String[] args) throws CloneNotSupportedException{
  29.                 Test1 p = new Test1("oh", 76);
  30.                 Test1 i = (Test1)p.clone();
  31.                 i.Pain();
  32.         }
  33. }
复制代码

输出:
  1. num = 76
  2. String and int args
  3. num = 76 s = oh
复制代码

clone 需要在类中重写后才能调用



toString();

返回该对象的字符串表示形式
  1.     public String toString() {
  2.         return getClass().getName() + "@" + Integer.toHexString(hashCode());
  3.     }
复制代码

在不重载的情况下, toString(); 语句就是按照上面的方法做的


(1).对象不一定会被回收。
(2).垃圾回收不是析构函数。
(3).垃圾回收只与内存有关。
(4).垃圾回收和finalize()都是靠不住的,只要JVM还没有快到耗尽内存的地步,它是不会浪费时间进行垃圾回收的。

引用链接:http://blog.csdn.net/carolzhang8406/article/details/6705831



线程相关方法


notify
不能重载,作用是唤醒一个等待(暂停)的线程


notifyAll();
唤醒所有等待(暂停)的线程


wait();
暂停该线程


finalize();
垃圾回收器准备释放内存时调用 finalize();






以上就是 Object 对象拥有的方法了,因为后面实例很长,就不放了






评分

参与人数 1鱼币 +3 收起 理由
小甲鱼 + 3

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-23 18:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表