zoukankan      html  css  js  c++  java
  • Java基础知识强化26:Object类之hashCode()方法、getClass()方法

    1. Object类的hashCode()方法,如下:

    public  int  hashCode():返回该对象的哈希码值,这个值和地址值有关,但是不是实际地址值(哈希码值是根据实际地址值转化过来的整数值),你可以理解为地址值。

    2. Object类的getClass()方法,如下:

    public  final  Class  getClass():返回此 Object 的运行时类(返回的类型是Class类,实际返回的是Class类的对象实体)

                          Class类的方法:

                                               public String getName():以 String 的形式返回此 Class 对象所表示的实体

    3 .案例

    (1)Student类

    1 package cn.itcast_01;
    2 
    3 public class Student extends Object {
    4 
    5 }

    (2)StudentTest类

     1 package cn.itcast_01;
     2 
     3 /*
     4  * Object:类 Object 是类层次结构的根类。每个类都使用 Object 作为超类。
     5  * 每个类都直接或者间接的继承自Object类。
     6  * 
     7  * Object类的方法:
     8  *         public int hashCode():返回该对象的哈希码值。
     9  *             注意:哈希值是根据哈希算法计算出来的一个值,这个值和地址值有关,但是不是实际地址值。
    10  *                        你可以理解为地址值。
    11  * 
    12  *        public final Class getClass():返回此 Object 的运行时类
    13  *            Class类的方法:
    14  *                public String getName():以 String 的形式返回此 Class 对象所表示的实体
    15  */
    16 public class StudentTest {
    17     public static void main(String[] args) {
    18         Student s1 = new Student();
    19         System.out.println(s1.hashCode()); // 11299397
    20         Student s2 = new Student();
    21         System.out.println(s2.hashCode());// 24446859
    22         Student s3 = s1;
    23         System.out.println(s3.hashCode()); // 11299397
    24         System.out.println("-----------");
    25 
    26         Student s = new Student();
    27         Class c = s.getClass();
    28         String str = c.getName();
    29         System.out.println(str); // cn.itcast_01.Student (包名+类名)--- 全路径名称
    30         
    31         //链式编程
    32         String str2  = s.getClass().getName();
    33         System.out.println(str2);
    34     }
    35 }
  • 相关阅读:
    168. Excel Sheet Column Title
    171. Excel Sheet Column Number
    264. Ugly Number II java solutions
    152. Maximum Product Subarray java solutions
    309. Best Time to Buy and Sell Stock with Cooldown java solutions
    120. Triangle java solutions
    300. Longest Increasing Subsequence java solutions
    63. Unique Paths II java solutions
    221. Maximal Square java solutions
    279. Perfect Squares java solutions
  • 原文地址:https://www.cnblogs.com/hebao0514/p/4820925.html
Copyright © 2011-2022 走看看