Java Object
2016-03-25 11:42
357 查看
public class Object { <span style="white-space:pre"> </span>//静态本地方法的声明并在静态代码块调用 <strong>private static native void registerNatives();</strong> static { registerNatives(); } public final native Class<?> getClass(); public native int hashCode(); <span style="white-space:pre"> </span>//该方法比较的是当前对象和其他对象的内存地址。所有要比较两个对象是否相等时候,要复现该对象的对应类定义的该方法 public boolean equals(Object obj) { return (this == obj); } //复制该对象,完全一致 protected native Object clone() throws CloneNotSupportedException; //需要复写该方法,不然就是hashcode值 public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); } //线程通信 public final native void notify(); //线程通信 public final native void notifyAll(); <pre name="code" class="java">//线程通信public final native void wait(long timeout) throws InterruptedException;
//线程通信public final void wait(long timeout, int nanos) throws InterruptedException { if (timeout < 0) { throw new IllegalArgumentException("timeout value is negative");
} if (nanos < 0 || nanos > 999999) { throw new IllegalArgumentException( "nanosecond timeout value out of range"); } if (nanos >= 500000 || (nanos != 0 && timeout == 0)) { timeout++; } wait(timeout); }
public final void wait() throws InterruptedException { wait(0); } //释放资源 protected void finalize() throws Throwable { }
这是Java中最顶层的类。Java中的任何类都是默认继承该objcet类的。对于该类的一些特征进行如下分析:
1、静态代码块、静态方法、静态属性
都是属于类的,可以直接用类名调用,当然任何该类对象都可以调用它们,这也是一种数据共享的方式。
2、静态代码块、构造方法的执行顺序
先执行静态代码块且只执行一次,再执行构造方法(每次创建实例都执行)。对象在实例化的过程中,调用无参数构造方法则执行无参构造。
如果使用有参构造实例化一个对象,则调用有参构造。
验证:定义一个person类
public class Person extends Object{ static String comm = null; static { System.out.println("-----------------Person静态代码块执行--------------"); comm = "共享变量的方式之一"; } public Person() { System.out.println("Person构造方法实例化"); } public Person(String name, String age) { this.age = age; this.name = name; System.out.println("Person有参构造方法实例化<<<<<<<<<<<"); } }在另外一个类中的main方法中进行测试:
public static void main(String[] args) { System.out.println("Study静态main执行"); Person p =new Person(); Person p2 =new Person(); Person p3=new Person(); Person p5=new Person("codeMonkey","99"); }
运行效果:
-----------------Person静态代码块执行--------------
Person构造方法实例化
Person构造方法实例化
Person构造方法实例化
Person有参构造方法实例化<<<<<<<<<<<
3、equals方法
如果没有复写该方法则默认是object'的方法,比较的是两个对象的地址。object中:
public boolean equals(Object obj) { return (this == obj); }
所有要比较两个对象是不是相等必须复写该方法和hashcode方法。如下以person类为例:
@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((age == null) ? 0 : age.hashCode()); result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false;
//还要比较属性值 Person other = (Person) obj; if (age == null) { if (other.age != null) return false; } else if (!age.equals(other.age)) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; }
4、拷贝方法,该方法可以复制出其类对象相同的对象。
该类需要复写clone方法如下以person为例:
@Override protected Object clone() throws CloneNotSupportedException { // return super.clone(); return this; }
测试方法:
Person p5=new Person("codeMonkey","99"); //Student s5=new Student(100000); try { Person p6= (Person) p5.clone(); System.out.println("Person对象测试拷贝方法"+("我是"+p6.getName()+"我"+p6.getAge()+"岁了是码神"+p6.comm)); System.out.println("Person对象clone() p5-->hashcode"+(p5.hashCode())); System.out.println("Person对象clone() p6-->hashcode"+(p6.hashCode())); System.out.println("Person对象clone() p5.equals(p6)"+(p5.equals(p6))); System.out.println("Person对象clone() p5==(p6)"+(p5==p6)); System.out.println("=========================================="); Person p7= (Person) p5.clone(); System.out.println("Person对象测试拷贝方法"+("我是"+p6.getName()+"我"+p6.getAge()+"岁了是码神"+p6.comm)); System.out.println("Person对象clone() p5-->hashcode"+(p5.hashCode())); System.out.println("Person对象clone() p7-->hashcode"+(p7.hashCode())); System.out.println("Person对象clone() p5.equals(p7)"+(p5.equals(p7))); System.out.println("Person对象clone() p5==(p7)"+(p5==p7)); } catch (CloneNotSupportedException e) { // TODO Auto-generated catch block e.printStackTrace(); }
结果:
-----------------Person静态代码块执行--------------
Person有参构造方法实例化<<<<<<<<<<<
Person对象测试拷贝方法我是codeMonkey我99岁了是码神共享变量的方式之一
Person对象clone() p5-->hashcode-1097621311
Person对象clone() p6-->hashcode-1097621311
Person对象clone() p5.equals(p6)true
Person对象clone() p5==(p6)true
==========================================
Person对象测试拷贝方法我是codeMonkey我99岁了是码神共享变量的方式之一
Person对象clone() p5-->hashcode-1097621311
Person对象clone() p7-->hashcode-1097621311
Person对象clone() p5.equals(p7)true
Person对象clone() p5==(p7)true
相关文章推荐
- const Pointers and Pointer to const Objects 常量指针和指向常量对象的指针
- const Pointers and Pointer to const Objects 常量指针和指向常量对象的指针
- 举例讲解Objective-C中@property属性的用法
- Object-C 之 可变参数
- Objective-C与JavaScript交互的那些事
- pip安装模块警告InsecurePlatformWarning: A true SSLContext object is not available.
- GitHub 上排名前 100 的 Objective-C 项目简介
- Objective-C Runtime 运行时之一:类与对象
- objective-C学习笔记(七) 字符串处理
- runtime(二)
- 对Object类中方法的理解
- Objective-C——retain/copy/assign/atomic/nonatomic/strong/weak/readonly/readwrite详解
- python使用PIL库报错,'NoneType' object has no attribute 'bands'
- 《编写高质量代码:改善Objective-C程序的61个建议》
- Objective-C Associated Objects 初识
- Object对象
- native c回调 java 中 自带参数 jclass和jobject的区别
- objective-C学习笔记(六)继承与多态
- iOS的Objective-C的工厂设计模式详解
- org.json.JSONException: Unterminated object at character 97 of