7.Object
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的更多相关文章
- CoreCLR源码探索(一) Object是什么
.Net程序员们每天都在和Object在打交道 如果你问一个.Net程序员什么是Object,他可能会信誓旦旦的告诉你"Object还不简单吗,就是所有类型的基类" 这个答案是对的 ...
- JavaScript Object对象
目录 1. 介绍:阐述 Object 对象. 2. 构造函数:介绍 Object 对象的构造函数. 3. 实例属性:介绍 Object 对象的实例属性:prototype.constructor等等. ...
- javascript之Object.defineProperty的奥妙
直切主题 今天遇到一个这样的功能: 写一个函数,该函数传递两个参数,第一个参数为返回对象的总数据量,第二个参数为初始化对象的数据.如: var o = obj (4, {name: 'xu', age ...
- c# 基础 object ,new操作符,类型转换
参考页面: http://www.yuanjiaocheng.net/webapi/config-webapi.html http://www.yuanjiaocheng.net/webapi/web ...
- APEX:对object中数据进行简单处理?
在Salesforce中,常常要对各种数据进行处理,已满足业务逻辑.本篇文章会介绍如何实现从object获取数据,然后将取得的数据进行一系列简单处理. 第一步:SongName__c 是一个新建的ob ...
- 笔记:Memory Notification: Library Cache Object loaded into SGA
笔记:Memory Notification: Library Cache Object loaded into SGA在警告日志中发现一些这样的警告信息:Mon Nov 21 14:24:22 20 ...
- Selenium的PO模式(Page Object Model)[python版]
Page Object Model 简称POM 普通的测试用例代码: .... #测试用例 def test_login_mail(self): driver = self.driver driv ...
- Object是什么
Object是什么 .Net程序员们每天都在和Object在打交道如果你问一个.Net程序员什么是Object,他可能会信誓旦旦的告诉你"Object还不简单吗,就是所有类型的基类" ...
- 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 ...
- CSharpGL - Object Oriented OpenGL in C#
Object Oriented OpenGL in C#
随机推荐
- iOS 使用xcode11新建项目
1. 首先打开Xcode11,然后使用command + shift + n 快捷键创建一个新的工程 选择 Single View App 完成之后点击next 2. 会弹出 Choose opt ...
- 拜占庭将军问题与CAP
1.拜占庭将军问题 拜占庭位于如今的土耳其的伊斯坦布尔,是东罗马帝国的首都.由于当时拜占庭罗马帝国国土辽阔,为了达到防御目的,每个军队都分隔很远,将军与将军之间只能靠信差传消息.在战争的时候,拜占庭军 ...
- IntelliJ中高效重构的 10 个快捷方式
前言 在日常的开发工作中,我们经常需要重构,重构可以让我们写出的代码更上一层楼.所以,我会借助IntelliJ提供的一些功能,帮助我高效进行重构.这里是我推荐10个快捷方式,也是我每天都在使用的,非常 ...
- Git + Jenkins 自动化 NGINX 发布简易实现
概述 之前基于 GitLab + Jenkins 实现了简单的 NGINX 的自动化发布. 具体包含如下的组件: GitLab 包括 GItLab 的 WebHook: Jenkins 及其插件: G ...
- Pytest插件pytest-repeat重复执行
Pytest插件pytest-repeat重复执行 安装 pip install pytest-repeat doc https://pypi.org/project/pytest-repeat/ h ...
- python学习day 02
昨日内容回顾 typora软件 1.作为一款逐年火爆的文本编辑器,深受IT行业的喜爱. 2.下载与安装: windows用群里发的软件 macOS下载地址:https://mac.qdrayst.co ...
- Vue13 样式动态绑定
1 class样式的动态绑定 1.1 说明 通过命令v-bind:class设置一个对象,动态切换class.可以简写为:class. class=""可以和:class=&quo ...
- springboot多模块controller访问的问题
参考 https://blog.csdn.net/qq_25091649/article/details/88429512 情况一:在主类直接写controller接口,能够访问到 @SpringBo ...
- (二) MdbCluster分布式内存数据库——分布式架构1
(二) MdbCluster分布式内存数据库--分布式架构1 分布式架构是MdbCluster的核心关键,业界有很多相关的实现,却很少有文章详细的解释每个架构实现背后的细节和这么做的原因.在Mdb ...
- .net 多地点计算中心点
1.需求产生 快到周末了,几个远在各个区的朋友想要聚餐,为了照顾到彼此的距离,决定计算一下所有人的中心点,至此需求产生,下面开始编写代码. 2.编写代码 1)新建一个控制台程序 在NuGet程序包管理 ...