zoukankan      html  css  js  c++  java
  • object类

    有类的父类

    常用方法 :

      1、   hashcode()       返回该对象的哈希吗值 

    public class demon1_HashCode {
    
        public static void main(String[] args) {
            Object o1 = new Object();
            int hc = o1.hashCode();   // 返回对象的哈希地址值
            System.out.println(hc);
            Student s1 = new Student("张三",23);    
            Student s2 = new Student("李四",24);
            System.out.println(s1.hashCode());      
            System.out.println(s2.hashCode());
        }
    }

      2、  getClass()          返回此对象的运行时类  

    public class demon2_getClass {
        /*
         * object 的getClass方法
         *    返回该对象的运行时类
         *    Class   一个 描述类的类
         */
        public static void main(String[] args) {
            Student s1 = new Student("张三",23);
            Class clazz = s1.getClass();      //  返回一个Class对象    
            String name = clazz.getName(); 
            System.out.println(name);          //   com.heima.object.Student    全类名
        }
    
    }

      3、  toString()   

        返回的是    getClass.getName()  + '@' +  Interger.toHexString(hashCode)

        一般需要重写

    public class demon3_ToString {
    
        public static void main(String[] args) {
            Student s1 = new Student("张三",23);
            System.out.println(s1.toString());   //   com.heima.object.Student@15db9742
        }
    }

      4、equals()   默认是比较地址值   ,一般需要重写,比较对象的属性值

    public class demon4_equals {
        public static void main(String[] args) {
            Student s1 = new Student("张三",23);
            Student s2 = new Student("张三",23);
            //System.out.println(s1.equals(s2)); // equals默认比较对象的地址值 ==
            System.out.println(s1==s2);  // ==  比较地址值
            //  默认比较对象的地址值没有意义,  需要重写,开发中更多的是比较属性值
            System.out.println(s1.equals(s2));   // 重写之后 比较的是 属性值
        }
    
    }
    public boolean equals(Object obj) {     // 重写的equals()
            Student s = (Student) obj;   // 多态   向下转型
            return this.name.equals(s.name)&&this.age == s.age;    //   
        }
    竹杖芒鞋轻胜马,一蓑烟雨任平生。 回首向来萧瑟处,也无风雨也无晴。
  • 相关阅读:
    Hibernate中日期(Date)类型 与oracle中日期的映射处理
    mssql注入经常使用的命令
    EL表达式遍历Map集合
    TomCat项目空间映射 配置
    JSP探针虚拟主机JSP环境检测工具
    什么是RSS
    大U盘容量的烦恼:copy 4G 以上单个文件时总是显示U盘已满
    Java初体验之bool
    何为Cookie
    转测试网站各项性能的31 个免费在线工具
  • 原文地址:https://www.cnblogs.com/yaobiluo/p/11302028.html
Copyright © 2011-2022 走看看