鱼C论坛

 找回密码
 立即注册
查看: 3188|回复: 1

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

[复制链接]
发表于 2017-8-14 18:49:33 | 显示全部楼层 |阅读模式

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

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

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

不能放弃
我已经受够什么事都只做一半的自己了  明明智商低,水平低还做特喵的什么鬼兼职,结果辞都辞不掉

Object 对象



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

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



getClass();


  1.      /**
  2.      * @return The {@code Class} object that represents the runtime
  3.      *         class of this object.
  4.      */
复制代码
  1. public final native Class<?> getClass();
复制代码

调用此方法会返回这个实例所对应的对象名
例如:

  1. public class TestObject {

  2.         public static void main(String[] args) {
  3.                 Student p = new Student(1, "小白");
  4.                 System.out.println(p.getClass());
  5.         }
  6.        
  7. }

  8. class Student{
  9.         int id;
  10.         String name;
  11.         public Person(int id, String name) {
  12.                 this.id = new Integer(id);
  13.                 this.name = new String(name);
  14.         }
  15. }
复制代码

结果是:
  1. class Student
复制代码


hashCode();

  1. /**
  2.      * Returns a hash code value for the object. This method is
  3.      * supported for the benefit of hash tables such as those provided by
  4.      * @return  a hash code value for this object.
  5.      * @see     java.lang.Object#equals(java.lang.Object)
  6.      * @see     java.lang.System#identityHashCode
  7.      */
  8.     public native int hashCode();
复制代码

计算一个实例的哈希码

即使一个类的两个实例的内部元素一样,哈希码也是不同的
例如:

  1. public class TestObject {

  2.         public static void main(String[] args) {
  3.                 Student p = new Student(1, "小白");
  4.                 Student p1 = new Student(1, "小白");
  5.                 System.out.println(p.hashCode());
  6.                 System.out.println(p1.hashCode());
  7.         }
  8.        
  9. }

  10. class Student{
  11.         int id;
  12.         String name;
  13.         public Person(int id, String name) {
  14.                 this.id = new Integer(id);
  15.                 this.name = new String(name);
  16.         }
  17. }
复制代码

结果:
  1. 2018699554
  2. 1311053135
复制代码


equals(Object obj);

  1.     public boolean equals(Object obj) {
  2.         return (this == obj);
  3.     }
复制代码

由 Java src 可知,Object 的是否相等的判断方法是对比在堆中的位置是否相等,所以一般需要自己重载
例子~

  1. public class TestObject {

  2.         public static void main(String[] args) {
  3.                 Student p = new Student(1, "小白");
  4.                 Student p1 = new Student(1, "小白");
  5.                 System.out.println(p.equals(p1));
  6.         }
  7.        
  8. }

  9. class Student{
  10.         int id;
  11.         String name;
  12.         public Person(int id, String name) {
  13.                 this.id = new Integer(id);
  14.                 this.name = new String(name);
  15.         }

  16.         @Override  // 这是重写的伪代码,会提示编译器检查代码的正确性
  17.         public boolean equals(Object obj) {
  18.                 /*Student person = (Student)obj;
  19.                 if(id == person.id && name == person.name)
  20.                         return true;
  21.                 return false;*/
  22.                 Student person = (Student)obj;
  23.                 if(id == person.id && name.equals(person.name)) return true;
  24.                 return false;
  25.         }
  26. }
复制代码

结果
  1. true
复制代码

评分

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

查看全部评分

本帖被以下淘专辑推荐:

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

使用道具 举报

 楼主| 发表于 2017-8-14 19:06:22 | 显示全部楼层
嗨呀,我感觉好迷茫啊..感觉人生一片灰暗...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-18 17:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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