ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Object类

2022-07-04 20:34:03  阅读:107  来源: 互联网

标签:对象 s1 Object System Student new public


Object类

  • 超类、基类,所以类的直接或间接父类,位于继承树的最顶层。

  • 任何类,如果没有书写extends显示继承某个类,都默认直接继承Object类,否则为间接继承。

  • Object类所定义的方法,是所有对象都具备的方法。

  • Object类型可以存储任何对象。

    • 作为参数,可接受任何对象。

    • 作为返回值,可返回任何对象。

       

getClass()方法

  • public final Class<?> getClass(){}

  • 返回引用中存储的实际对象类型。

  • 应用:通常用于判断两个引用中实际存储对象类型是否一致。

代码示例:

 public class Student {
     private String name;
     private int age;
 ​
     public Student() {
    }
 ​
     public Student(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 class TestStudent {
     public static void main(String[] args) {
         Student s1 = new Student("aaa",20);
         Student s2 = new Student("bbb",18);
       
         //getClass方法
         //判断s1和s2是否是同一个类型
         Class class1 = s1.getClass();
         Class class2 = s2.getClass();
         if (class1 == class2){
             System.out.println("是");
        }else {
             System.out.println("否");
        }
    }
 }

 

hashCode()方法

  • public int hashCode(){}

  • 返回该对象的哈希码值。

  • 哈希值根据 对象的地址字符串数字 使用hash算法计算出来的int类型的数值。

  • 一般情况下相同对象返回相同哈希码。

    代码示例:

     public class TestStudent {
         public static void main(String[] args) {
             Student s1 = new Student("aaa",20);
             Student s2 = new Student("bbb",18);
             
             //hashCode方法
             System.out.println(s1.hashCode());
             System.out.println(s2.hashCode());
     ​
             Student s3 = s1;
             System.out.println(s3.hashCode());

    结果:

     

     

 

toString()方法

  • public String toString(){}

  • 返回该对象的字符串(表现形式)。

  • 可以根据程序需求覆盖该方法,如:展示对象各个属性值。

    代码示例:

     public class TestStudent {
         public static void main(String[] args) {
             Student s1 = new Student("aaa",20);
             Student s2 = new Student("bbb",18);
     ​
             //toString方法
             System.out.println(s1.toString());
             System.out.println(s2.toString());
        }
     }

结果:

 

 

如果对返回结果不满意,可以重写方法

 @Override
     public String toString() {
         return "Student{" +
                 "name='" + name + '\'' +
                 ", age=" + age +
                 '}';
    }

结果:

 

 

 

equals()方法

  • public boolean equals(Object obj){}

  • 默认实现为(this == obj),比较两个对象的地址是否相同。

  • 可进行覆盖,比较两个对象的内容是否相同。

    代码示例:

     public class TestStudent {
         public static void main(String[] args) {
             Student s1 = new Student("aaa",20);
             Student s2 = new Student("bbb",18);
             Student s3 = s1;
     ​
             //equals方法:比较两个对象地址是否相同
             System.out.println(s1.equals(s2));
             System.out.println(s1.equals(s3));
        }
     }

    结果:第一个s1与s2的比较结果为false;第二个s1与s3的比较结果为true。

     

equals()方法覆盖步骤

  1. 比较两个引用是否指向同一个对象。

  2. 判断obj是否为null。

  3. 判断两个引用指向的实际对象类型是否一致。

  4. 强制类型转换。

  5. 依次比较各个属性值是否相同。

 

finalize()方法

  • 当对象被判定为垃圾对象时,由JVM自动调用此方法,用以标记垃圾对象,进入回收队列。

  • 垃圾对象:没有有效引用指向此对象,为垃圾对象。

  • 垃圾回收:由GC销毁垃圾对象,释放数据存储空间。

  • 自动回收机制:JVM的内存耗尽,一次性回收所有垃圾对象。

  • 手动回收机制:使用System.gc();通知JVM执行垃圾回收。

 Student s1 = new Student("aaa",20);
 Student s2 = new Student("bbb",18);
 Student s3 = new Student("ccc",18);
 System.gc();

结果:没有回收

 new Student("aaa",20);
 new Student("bbb",18);
 new Student("ccc",18);
 System.gc();

结果:成功回收

原因:只new对象,栈里没有引用,堆里面的对象用完被回收。一个堆空间中的对象没有被栈空间变量指向时,这个对象会等待被java回收。

标签:对象,s1,Object,System,Student,new,public
来源: https://www.cnblogs.com/zyhls-00688/p/16444310.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有