码迷,mamicode.com
首页 > 其他好文 > 详细

object类的使用

时间:2015-01-09 22:11:37      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

/*
object类,所有的类都继承object类
tostring()对象的字符串的描述
hashcode()返回对象的哈希码
equals()判断两个对象内容是否相等
重写equals()方法,必须重写hashcode()方法
clone()创建并返回此对象的副本
要想一个类的对象能够克隆,必须实现两点
1:实现Cloneable接口(声明式接口,没要实现的方法)
2:重写object继承的clone()方法
*/

class
people implements Cloneable { private String name; private int age; public people() { } public people(String name,int age) { this.name=name; this.age=age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String toString() { //重写父类的tostring(),不然返回对象名+哈希码(十六进制) return "name:"+this.getName()+",age"+this.getAge(); } /* public String toString() { // TODO Auto-generated method stub return super.toString(); }*/ @Override public boolean equals(Object obj) { if (obj instanceof people) { //是否为people people people=(people) obj; if (this.getName().equals(people.getName())&&(this.getAge()==people.getAge())) { return true; } else { return false; } } else { return false; } } @Override public int hashCode() { // TODO Auto-generated method stub return this.getName().hashCode()+this.getAge(); } @Override protected Object clone() throws CloneNotSupportedException { // TODO Auto-generated method stub return super.clone(); } } public class main { public static void main(String[] args) { people aPeople=new people("jack", 23); System.out.println(aPeople); //people@59ec59df,没重写tostring() System.out.println(aPeople); //name:jack,age23,重写tostring() System.out.println("aPeople的hashCode"+aPeople.hashCode());//aPeople的hashCode1508661727 //打印出的是十进制的哈希码,和没重写tostring()方法的十六进制值相等 //两个对象equales,他们的hashcode一定相等 //两个对象不相等,他们的hangcode有可能相等 people aPeople1=new people("jack", 23);//new的话是一个新对象 System.out.println("aPeople1的hashCode"+aPeople1.hashCode());//aPeople1的hashCode1343958201 System.out.println(aPeople==aPeople1);//地址不相同,返回false System.out.println(aPeople.equals(aPeople1));//返回false ? //说明:equals比较两者的内容是否相等,看上去两者内容相等,当没有重写它的equals方法时候, //默认比较的是他们的hashcode,对于上面2个对象,hashcode肯定不同,故返回false System.out.println(aPeople.equals(aPeople1));//重写equals后return true String a=new String("jack"); String b=new String("jack"); System.out.println(a==b); //return false; System.out.println(a.equals(b)); //return true; ? System.out.println(a.hashCode()==b.hashCode()); //return true; ? //这边为什么又是true呢?因为String类(特殊的类)重写了hachcode和equals方法 System.out.println(System.identityHashCode(a)); System.out.println(System.identityHashCode(b)); //重写前的hashcode值是不同的 //people p3=aPeople1; //这样clone的话p3改变的话对apeople1也有影响 try { people p4=(people) aPeople1.clone(); p4.setName("xiaoming"); System.out.println(p4); //这样的话是copy的副本,输出结果不一样 System.out.println(aPeople1); //clone,改变p4对apeople1没影响 } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

 

object类的使用

标签:

原文地址:http://www.cnblogs.com/linhong/p/4214152.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!