Object类

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

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

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

Object类型可以存储任何对象

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

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

getClass()方法

public final Class<?> getClass(){}

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

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

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

hashCode()方法

public int hashCode(){}

返回该对象的哈希码值

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

一般情况下相同对象返回相同哈希值

public class TesthashCode {
   public static void main(String[] args) {
       Student s1 = new Student("aaa", 20);
       Student s2 = new Student("bbb", 22);

       System.out.println(s1.hashCode());
       System.out.println(s2.hashCode());
       Student s3 = s1;
       System.out.println(s3.hashCode());
  }
}
/*
460141958
1163157884
460141958
*/

toString()方法

public String toString(){}

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

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

可根据需要进行重写

equals()方法

public boolean equals (Object obj){}

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

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

equals()方法覆盖步骤

  • 比较两个引用是否指向同一个对象

  • 判断obj是否为null

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

  • 强制类型转换

  • 依次比较各个属性值是否相同

    public boolean equals(Object obj) {
       if (this==obj){
           return true;
      }
       if (obj==null)
           return false;
//       if (this.getClass()==obj.getClass())
//       instenceof 判断对象是否是某种类型
       if (obj instanceof Student){
           Student s = (Student) obj;
           if (this.name.equals(s.getName()) && this.age==s.getAge()){
               return true;
          }
      }
       return false;
  }
Student s3 = new Student("xiix",10);
Student s4 = new Student("xiix",10);
System.out.println(s3.equals(s4));
//true

finalize()方法

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

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

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

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

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

 protected void finalize() throws Throwable {
       System.out.println(this.name+"对象被回收");
  }
public class TestFinalize {
   public static void main(String[] args) {
//       Student s1 = new Student("aa", 20);
////       Student s2 = new Student("bb", 20);
////       Student s3 = new Student("cc", 20);
////       Student s4 = new Student("ee", 20);
////       Student s5 = new Student("ff", 20);
       new Student("aa", 20);
       new Student("bb", 20);
       new Student("cc", 20);
       new Student("ee", 20);
       new Student("ff", 20);
       //回收垃圾
       System.gc();
       System.out.println("回收垃圾");
  }
}
/*
回收垃圾
aa对象被回收
ff对象被回收
ee对象被回收
cc对象被回收
bb对象被回收
*/
 

7.Object的更多相关文章

  1. CoreCLR源码探索(一) Object是什么

    .Net程序员们每天都在和Object在打交道 如果你问一个.Net程序员什么是Object,他可能会信誓旦旦的告诉你"Object还不简单吗,就是所有类型的基类" 这个答案是对的 ...

  2. JavaScript Object对象

    目录 1. 介绍:阐述 Object 对象. 2. 构造函数:介绍 Object 对象的构造函数. 3. 实例属性:介绍 Object 对象的实例属性:prototype.constructor等等. ...

  3. javascript之Object.defineProperty的奥妙

    直切主题 今天遇到一个这样的功能: 写一个函数,该函数传递两个参数,第一个参数为返回对象的总数据量,第二个参数为初始化对象的数据.如: var o = obj (4, {name: 'xu', age ...

  4. c# 基础 object ,new操作符,类型转换

    参考页面: http://www.yuanjiaocheng.net/webapi/config-webapi.html http://www.yuanjiaocheng.net/webapi/web ...

  5. APEX:对object中数据进行简单处理?

    在Salesforce中,常常要对各种数据进行处理,已满足业务逻辑.本篇文章会介绍如何实现从object获取数据,然后将取得的数据进行一系列简单处理. 第一步:SongName__c 是一个新建的ob ...

  6. 笔记:Memory Notification: Library Cache Object loaded into SGA

    笔记:Memory Notification: Library Cache Object loaded into SGA在警告日志中发现一些这样的警告信息:Mon Nov 21 14:24:22 20 ...

  7. Selenium的PO模式(Page Object Model)[python版]

     Page Object Model 简称POM  普通的测试用例代码: .... #测试用例 def test_login_mail(self): driver = self.driver driv ...

  8. Object是什么

    Object是什么 .Net程序员们每天都在和Object在打交道如果你问一个.Net程序员什么是Object,他可能会信誓旦旦的告诉你"Object还不简单吗,就是所有类型的基类" ...

  9. a different object with the same identifier value was already associated with the session:

    hibernate操作: 实例化两个model类,更新时会提示  a different object with the same identifier value was already assoc ...

  10. CSharpGL - Object Oriented OpenGL in C#

    Object Oriented OpenGL in C#

随机推荐

  1. [Leetcode] 寻找数组的中心索引

    题目 代码 class Solution { public: int pivotIndex(vector<int>& nums) { int right=0; for(auto i ...

  2. python利用matplotlib生成迷宫

    起因 我想要写一个项目叫python迷宫游戏,需求是玩家能和机器对抗率先走出迷宫,至少要有两个等级的电脑. 慢慢来,首先迷宫游戏需要有一个迷宫并展示出来,这便是这篇博客的目的 假设迷宫使用0表示点,1 ...

  3. MySQL 更新数据 不同条件(批量)更新不同值

    一般在更新时会遇到以下场景:1.全部更新:2.根据条件更新字段中的某部分内容:3.根据不同的条件更新不同的值,以下是几种场景中常用的update方法. 一.方法分类 二.具体用法 (1)根据条件更新值 ...

  4. py教学 之字符串处理·····

    访问字符串中的值 Python 不支持单字符类型,单字符在 Python 中也是作为一个字符串使用. Python 访问子字符串,可以使用方括号 [] 来截取字符串,字符串的截取的语法格式如下: 变量 ...

  5. 读Java8函数式编程笔记08_测试、调试和重构

    1. Lambda表达式的单元测试 1.1. 单元测试是测试一段代码的行为是否符合预期的方式 1.2. Lambda表达式没有名字,无法直接在测试代码中调用 1.2.1. 将Lambda表达式放入一个 ...

  6. JSP第八次作业

    数据库test 中建个表 stu(stuid 主键 自动增长 ,用户名,密码,年龄) 1.设计一个注册页面,实现用户注册功能2.设计一个登陆页面,实现用户名密码登陆3.两个页面可以互相超链接 1 pa ...

  7. rust 网上资料记录(自用)

    最近要学嵌入式的rust,记录一些资料的url,方便自己查阅 书籍 常用的: rust圣经(不是权威指南那本)https://course.rs/ 中文 rust 参考手册 https://rustw ...

  8. 周末折腾了两天,踩了无数个坑,终于把win7装成了centos7

    上周五的时候,突发奇想,想把自己的Thinkpad E430C的操作系统装成linux. 熟悉电脑的都知道Thinkpad E430C很古老了,现在算来从2012年买来,到现在已经经历了10个年头了. ...

  9. Windows下x86和x64平台的Inline Hook介绍

    前言 我在之前研究文明6的联网机制并试图用Hook技术来拦截socket函数的时候,熟悉了简单的Inline Hook方法,但是由于之前的方法存在缺陷,所以进行了深入的研究,总结出了一些有关Windo ...

  10. 如何通过Java 代码设置 Word 文档页边距

    页边距是指页面的边线到文字的距离.通常可在页边距内部的可打印区域中插入文字和图形,也可以将某些项目放置在页边距区域中(如页眉.页脚和页码等).在我们用的Word文档中,都会设置页边距统一标准格式,页边 ...