java中的equals用法

在Object 类中定义有:

1、public boolean equals(Object object )方法提供定义对象是否“相等”逻辑。

2、Object的equals方法定义为:x.equals(y)当x和y是同一个对象的引用时,返回true,否则返回false

3、在其他一些类中,例如String Date等,重写了Object的equals方法,调用这些类的equals方法,当x和y所引用的对象是同一类对象且属性内容相等时

(并不一定是相同对象),return true; 否则的话,return false;

4、当然,可以根据需要在用户自己定义重写equal方法。

举例:

public class TestEquals{

    public  static void main(String[] args) {
                Cat c1 = new Cat(1,2,3);
                Cat c2 = new Cat(1,2,3);
                System.out.println(c1 == c2);//false
                System.out.println(c1.equals(c2));//true
        }
    }
    class Cat{
        int color;
        int height,weight;
        public Cat(int color, int height, int weight){
            this.color = color;
            this.height = height;
            this.weight = weight;
        }
    public boolean equals​(Object obj){
        if(obj == null) return false;
        else{
            if(obj instanceof Cat){/*if obj is  a object of Cat ,return true ,else if obj is not a object of Cat or is null,return flase*/
            Cat c = (Cat)obj;
            if(c.color == this.color && c.height == this.height && c.weight == this.weight)
            return true;
            }
        }
    }

return false;

}

运行结果:

false
       true





本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/274147.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

根据图片获得配色方案_配色系列(1)—从图片中获得配色灵感

根据图片获得配色方案前言 (Foreword) When we start designing mobile web pages, we always need to determine the color scheme of the web page first. Well, at this time, unless the customer proposes a color scheme, most of the situations we have to face is to …

七大查找算法

1. 顺序查找2. 二分查找3. 插值查找4. 斐波那契查找5. 树表查找6. 分块查找7. 哈希查找 查找是在大量的信息中寻找一个特定的信息元素,在计算机应用中,查找是常用的基本运算,例如编译程序中符号表的查找。本文简单概括性的介绍了常见的七种查…

HA2795Billboard 可用线段树

#include<iostream>using namespace std;#include<cstdio>#include<algorithm>#define maxn 200005int h,n,w;int root[maxn<<4];int ans;//标记void make_tree(int l,int r,int rt){if(lr){root[rt]w;return ;}int mid(rl)/2;make_tree(l,mid,rt*2);m…

axure下拉列表框单选框_如何在Axure中创建下拉菜单和组合框

axure下拉列表框单选框First, let’s clarify what exactly is a dropdown menu, and what is a combo box, aren’t they the same? Well … no, not really, let me explain.首先&#xff0c;让我们弄清楚什么是下拉菜单&#xff0c;什么是组合框&#xff0c;不是吗&#xf…

Android 第一课 Activity

以下说明基于Android Studio&#xff0c;简称AS。&#xff08;红色字体为自行添加&#xff0c;注在醒目&#xff09; 具体包括&#xff1a;创建活动创建项目 加载布局 在AndroidManifest文件中注册 活动中使用&#xff08;提醒&#xff09;Toast 在活动使用&#xff08;菜…

figma设计_一种在Figma中跟踪设计迭代的简单方法

figma设计As designers, telling a good story is always part of the job. A great story engages the client with the journey of decision making; it shows your team the breadth and depth of the exploration; it also encourages us to reflect on our own design pro…

Android 第二课 Intent

上一节&#xff0c;掌握了活动的创建&#xff0c;但是在启动器中点击应用的图标只会进入到该应用的主活动&#xff0c;那么&#xff0c;如何从主活动跳转到其他活动呢&#xff1f;显式IntentIntent有多个构造函数&#xff0c;其中一个是Intent(Context packContext,Class<?…

ok~加油!

你有梦想&#xff0c;放手去做&#xff01;转载于:https://www.cnblogs.com/the-one/p/3217124.html

latex 插图 上下放_专辑插图中上下文中的文本

latex 插图 上下放Especially if, like me, you’re not properly educated in the world of visual design, typography, and all those other things that a formal education can bring. We’re kind of playing around until something fits right, and doesn’t feel jarr…

亡羊补牢,为时不晚?

亡羊补牢&#xff0c;为时不晚 人总是想着第二次比第一次做的好&#xff0c;可是会一直有第二次机会吗&#xff1f;当你思考好&#xff0c;决定做一件事情的时候&#xff0c;为什么不坚持下去呢&#xff1f;当你思考好&#xff0c;记住坚持到底就会胜利。祝福自己&#xff0…

2013年7月份第4周51Aspx源码发布详情

大型企业通用管理ERP源码 2013-7-26 [VS2010]2013.7.4更新内容&#xff1a;1.修复决策模式-客户等级不能保存问题。2.修复企业知识库有报错问题。3.修复运营模式-人力资源分析模块-在部分模块点击查询后&#xff0c;水晶报表显示无法加载文件问题&#xff0c;4.修复行政办公模…

视觉感知_产品设计中的视觉感知

视觉感知The role of the UX designer has evolved immensely over time, but at its core, it remains the same- UX设计人员的角色随着时间的流逝而发生了巨大的变化&#xff0c;但从本质上讲&#xff0c;它保持不变- to deliver information to users in an effective mann…

Android 第三课 Activity的生命周期

新建项目ActivityLifeCycleTest&#xff0c;创建主活动后&#xff0c;再新建两个子活动--NormalActivity和DialogActivity。 现在活动及其对应布局文件创建完毕。 编辑normal_layout.xml文件&#xff0c;代码如下&#xff1a; <?xml version"1.0" encoding"…

转载:Apache commons开源工具简介

Apache Commons是一个非常有用的工具包&#xff0c;解决各种实际的通用问题&#xff0c;下面是一个简述表&#xff0c;详细信息访问http://jakarta.apache.org/commons/index.html BeanUtilsCommons-BeanUtils 提供对 Java 反射和自省API的包装 BetwixtBetwixt提供将 JavaBean …

pb 插入报列在此处不_获取有关[在此处插入问题]的事实

pb 插入报列在此处不Twitter’s recent move to put notices on tweets themselves is one of the most controversial social media features during our times. As a design technologist, I can’t help but wonder the decision-making process behind it. It’s a perfect…

设计模式_单实体模式

Singleton 三要素&#xff1a;private 构造函数、 public 静态方法、 public 静态变量 单实例模式的三种线程安全实现方式&#xff08;&#xff23;&#xff0b;&#xff0b;&#xff09; &#xff11; 懒汉模式&#xff1a;即第一次调用该类实例的时候才产生一个新的该类实例…

Android 第四课 活动的启动模式

启动模式分为4种&#xff0c;分别为standard&#xff0c;singleTop&#xff0c;singleTask&#xff0c;和singleInstance我们可以在AndroidManifest.xml中通过给<activity>标签指定android:launchMode属性来选择启动模式。standard standard是活动默认的启动模式。在stan…

c++编写托管dll_教程:如何编写简单的网站并免费托管

c编写托管dll本教程适用于谁&#xff1f; (Who is this tutorial for?) This tutorial assumes no prior knowledge and is suitable for complete beginners as a first project 本教程假定您没有先验知识&#xff0c;并且适合初学者作为第一个项目 您将需要什么 (What you w…

浅述WinForm多线程编程与Control.Invoke的应用

在WinForm开发中&#xff0c;我们通常不希望当窗体上点了某个按钮执行某个业务的时候&#xff0c;窗体就被卡死了&#xff0c;直到该业务执行完毕后才缓过来。一个最直接的方法便是使用多线程。多线程编程的方式在WinForm开发中必不可少。本文介绍在WinForm开发中如何使用多线程…

Android 第五课 常用控件的使用方法(TextView、Button、EditView、 ImageView、 ProgressBar、 ProgressDialog等)

总结&#xff1a;见名知意 TextView&#xff1a; Button: EditView: ImageView: ProgressBar: ProgressDialog和AlertDialog有些类似&#xff0c;都可以再界面弹出对话框&#xff0c;都能够屏蔽其他控件的交互能力&#xff0c;用法也类似。 我们还发现ProgressDialog和AlertDia…