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.pswp.cn/news/274147.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274147.shtml
英文地址,請注明出處:http://en.pswp.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…