zoukankan      html  css  js  c++  java
  • Object类

    Object类

    hashCode()方法
    • public int hashCode()
    • 返回该对象的哈希码值,默认情况下,该方法会根据对象的地址来计算
    • 不同对象的,hashCode()一般来说不会相同,但是同一个对象的hashCode()值肯定相同
    实例
    package com.mephisto.object;
    
    import com.mephisto.bean.Student;
    
    public class DemoHashCode {
    
    	public static void main(String[] args) {
    		Object object = new Object();
    		int hashCode = object.hashCode();
    		System.out.println(hashCode);
    		
    		Student student = new Student("张三",18);
    		Student student2 = new Student("李四",19);
    		
    		System.out.println(student.hashCode());
    		System.out.println(student2.hashCode());
    	}
    
    }
    
    
    输出结果
    2018699554
    1311053135
    118352462
    
    getClass()
    • public final class getClass()

    • 返回此Object的运行时类

    • 可以通过Class类中的一个方法,获取对象的真实类的全名称

      • public String getName()
    实例
    package com.mephisto.object;
    
    import com.mephisto.bean.Student;
    
    public class DemoGetClass {
    	
    	public static void main(String[] args) {
    		Student student = new Student();
    		Class clazz = student.getClass();	// 获取该对象的字节码文件
    		String name = clazz.getName();		// 获取名称
    		System.out.println(name);
    		
    	}
    
    }
    
    
    输出结果
    com.mephisto.bean.Student
    
    toString()
    • public String toString()

    • 返回该对象的字符串表示

    实例
    package com.mephisto.object;
    
    import com.mephisto.bean.Student;
    
    /**
     * 
     * @author Administrator
     *
     */
    public class DemoToString {
    	/**
    	 * public String toString() {
    	 * 	  // Integer.toHexString() 返回整数参数的字符串表示形式,作为16位中的无符号整数.
         *    return getClass().getName() + "@" + Integer.toHexString(hashCode());  
         *	}
    	 * @param args
    	 * 左边:类名
    	 * 中间:@
    	 * 右边:hashCode的十六进制表现形式
    	 * toString方法的作用:可以更方便的显示属性值
    	 * getXXX方法是为了获取值,也可以显示复制,或其他操作
    	 */
    	public static void main(String[] args) {
    		/* 在Student.class重写toString
    		 * 	@Override
    		 *  public String toString() {
    		 *	return "我的姓名: " + name + " 我的年龄: " + age;
    		 *	}
    		 */
    		Student stu = new Student("张三", 23);
    //		String str = stu.toString();
    		System.out.println(stu.toString());
    		System.out.println(stu);	// 如果之家打印对象的引用,会默认调用toString()方法
    	}
    }
    
    输出结果
    我的姓名: 张三 我的年龄: 23
    我的姓名: 张三 我的年龄: 23
    
    equals()
    • 提示其他某个对象是否与该对象相等
    • 默认情况下比较是对象的引用是否相等
    • 由于比较对象的引用没有意义,一般重写该方法
    实例
    package com.mephisto.object;
    
    import com.mephisto.bean.Student;
    
    /**
     * 
     * @author Administrator
     *
     */
    public class DemoEquals {
    	/**
    	 * public boolean equals(Object obj) {
    	 * 		return (this == obj);
         * }
         * 
         * Obeject中的equals方法比较对象的地址值,没什么意义,需要重写
         * 通常比较对象中的属性值, 认为相同属性是一个对象
         * 
         * 重写Student.class equals方法
         * 
         * 	@Override
    	 *	public boolean equals(Object obj) {
    	 *		Student student = (Student)obj;   // 向下转型  
    	 *		return this.name.equals(student.name) && this.age == student.age;
    	 *	}
    	 */
            
    	public static void main(String[] args) {
    		Student student = new Student("张三",23);
    		Student student2 = new Student("张三",23);
    		/* 比较两个对象是否相等 */
    		boolean b = student.equals(student2);
    		System.out.println(student == student2);
    		System.out.println(b);  // 重写之后比较的是对象的属性值
    	}
    }
    
    输出结果
    false
    true
    
    ==equals方法的区别
    • ==时一个比较运算运算符,既可以比较基本数据类型,也可以比较引用数据雷响,基本数据类型比较的是值,引用数据类型比较的是地址值

    • equals方法是一个方法, 只能比较引用数据类型,比较的是地址值,底层依赖的是==号所有对象都会继承Object类中的方法,若果没有重写Object类中的equals方法,equals方法和==号比较引用数据类型无区别,但是重写equals方法比较的是对象中的属性

  • 相关阅读:
    使用EF取数据库返回的数据
    关注博客分类
    Natas23 Writeup(php弱类型)
    Natas22 Writeup(header重定向、burp截取抓包)
    Natas21 Writeup(共用session、session注入)
    Natas20 Writeup(Session登录,注入参数)
    Natas19 Writeup(Session登录,常见编码,暴力破解)
    Natas18 Writeup(Session登录,暴力破解)
    Natas17 Writeup(sql盲注之时间盲注)
    Natas15 Writeup(sql盲注之布尔盲注)
  • 原文地址:https://www.cnblogs.com/mephisto03/p/9385016.html
Copyright © 2011-2022 走看看