if (this == &other) { return *this; } 这步是判断啥?

时间: 2024-05-31 20:13:08 浏览: 12
这是在C++中实现的赋值运算符重载函数中的一部分,用于检查自我赋值并避免出现问题。在这个条件语句中,"this"是指向当前对象的指针,"&other"是指向传递给函数的另一个对象的指针。如果这两个指针所指向的对象是同一个对象,那么说明这是自我赋值,这时候直接返回*this即可,避免出现错误。否则,可以继续进行赋值操作。
相关问题

@Override public boolean equals(Object that) { if (this == that) { return true; } if (that == null) { return false; } if (getClass() != that.getClass()) { return false; } ApartmentAdmin other = (ApartmentAdmin) that; return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId())) && (this.getUserName() == null ? other.getUserName() == null : this.getUserName().equals(other.getUserName())) && (this.getPassword() == null ? other.getPassword() == null : this.getPassword().equals(other.getPassword())) && (this.getRoleId() == null ? other.getRoleId() == null : this.getRoleId().equals(other.getRoleId())) && (this.getHeadimg() == null ? other.getHeadimg() == null : this.getHeadimg().equals(other.getHeadimg())) && (this.getPhone() == null ? other.getPhone() == null : this.getPhone().equals(other.getPhone())) && (this.getSex() == null ? other.getSex() == null : this.getSex().equals(other.getSex())) && (this.getBuildingNo() == null ? other.getBuildingNo() == null : this.getBuildingNo().equals(other.getBuildingNo())) && (this.getCreateTime() == null ? other.getCreateTime() == null : this.getCreateTime().equals(other.getCreateTime())) && (this.getIsLock() == null ? other.getIsLock() == null : this.getIsLock().equals(other.getIsLock())); }请详细解释每一行代码

这是一个Java类中的重写 equals 方法,用于比较两个对象是否相等。具体解释如下: 1. 第一行 @Override 注解表示该方法是重写了父类的 equals 方法。 2. 第二行开始是方法的定义,public 表示该方法是公共的,可以被其他类访问和调用;boolean 表示该方法返回一个布尔类型的值,即判断两个对象是否相等,返回true表示相等,返回false表示不相等。 3. 参数 Object that 表示传入该方法的对象,Object 是 Java 中所有类的父类,因此该方法可以接收任何类型的对象作为参数。 4. 第四行 if (this == that) 判断传入的对象是否与当前对象相同,如果是,则直接返回 true,表示两个对象相等。 5. 第五行 if (that == null) 判断传入的对象是否为 null,如果是,则直接返回 false,表示两个对象不相等。 6. 第六行 if (getClass() != that.getClass()) 判断传入的对象是否属于当前对象所属的类,如果不是,则直接返回 false,表示两个对象不相等。 7. 第七行到第十四行是比较两个对象中的属性是否相等,如果相等,则返回 true,否则返回 false。其中使用了三元运算符,如果属性为 null,则需要使用 equals 方法进行判断,而不能直接使用 == 进行判断。 8. 最后,该方法判断的属性包括:id、用户名、密码、角色id、头像、电话、性别、楼号、创建时间、是否锁定。

完成下列的 String 类,并在主函数 main( )中测试它。 class String { public: String(const char *str = NULL); // constructor String(const String &other); // copy constructor ~ String(void); // destructor String & operate =(char *str); String & operate =(const String &other);// 重载=运算符 int operator==(String &other); // 重载==运算符 int operator==(char *str); private: char *m_data; // used for storing the string int length; };

以下是 String 类的实现以及在主函数 main() 中的测试。 ```cpp #include <iostream> #include <cstring> using namespace std; class String { public: String(const char *str = NULL); String(const String& other); ~String(void); String& operator=(char *str); String& operator=(const String& other); int operator==(String& other); int operator==(char *str); private: char *m_data; int length; }; String::String(const char* str) { if (str != NULL) { length = strlen(str); m_data = new char[length + 1]; strcpy(m_data, str); } else { length = 0; m_data = new char[1]; *m_data = '\0'; } } String::String(const String& other) { length = other.length; m_data = new char[length + 1]; strcpy(m_data, other.m_data); } String::~String(void) { delete[] m_data; } String& String::operator=(char *str) { if (m_data == str) { return *this; } if (str != NULL) { length = strlen(str); char *temp = new char[length + 1]; strcpy(temp, str); delete[] m_data; m_data = temp; } else { length = 0; delete[] m_data; m_data = new char[1]; *m_data = '\0'; } return *this; } String& String::operator=(const String& other) { if (m_data == other.m_data) { return *this; } length = other.length; char *temp = new char[length + 1]; strcpy(temp, other.m_data); delete[] m_data; m_data = temp; return *this; } int String::operator==(String& other) { return strcmp(m_data, other.m_data) == 0; } int String::operator==(char *str) { return strcmp(m_data, str) == 0; } int main() { String s1("hello"); String s2(s1); String s3("world"); String s4; s4 = s1; cout << s1.operator==("hello") << endl; // 1 cout << s1.operator==(s2) << endl; // 1 cout << s1.operator==(s3) << endl; // 0 cout << s1.operator==(s4) << endl; // 1 return 0; } ```

相关推荐

这段函数代表什么 public int compareTo(WindowContainer other) { Slog.d("huangbg", Log.getStackTraceString( new Throwable())); Slog.d("huangbg","other = " + other); if (this == other) { Slog.d("huangbg"," compareTo 929"); return 0; } Slog.d("huangbg"," mParent = " + mParent); if (mParent != null && mParent == other.mParent) { Slog.d("huangbg"," compareTo 934"); final WindowList<WindowContainer> list = mParent.mChildren; return list.indexOf(this) > list.indexOf(other) ? 1 : -1; } final LinkedList<WindowContainer> thisParentChain = mTmpChain1; final LinkedList<WindowContainer> otherParentChain = mTmpChain2; try { getParents(thisParentChain); other.getParents(otherParentChain); // Find the common ancestor of both containers. WindowContainer commonAncestor = null; WindowContainer thisTop = thisParentChain.peekLast(); WindowContainer otherTop = otherParentChain.peekLast(); while (thisTop != null && otherTop != null && thisTop == otherTop) { commonAncestor = thisParentChain.removeLast(); otherParentChain.removeLast(); thisTop = thisParentChain.peekLast(); otherTop = otherParentChain.peekLast(); } Slog.d("huangbg"," commonAncestor = " + commonAncestor); // Containers don't belong to the same hierarchy??? if (commonAncestor == null) { throw new IllegalArgumentException("No in the same hierarchy this=" + thisParentChain + " other=" + otherParentChain); } // Children are always considered greater than their parents, so if one of the containers // we are comparing it the parent of the other then whichever is the child is greater. if (commonAncestor == this) { Slog.d("huangbg"," compareTo 965"); return -1; } else if (commonAncestor == other) { Slog.d("huangbg"," compareTo 968"); return 1; } Slog.d("huangbg"," compareTo 971"); // The position of the first non-common ancestor in the common ancestor list determines // which is greater the which. final WindowList<WindowContainer> list = commonAncestor.mChildren; return list.indexOf(thisParentChain.peekLast()) > list.indexOf(otherParentChain.peekLast()) ? 1 : -1; } finally { mTmpChain1.clear(); mTmpChain2.clear(); } }

class Complex implements Cloneable{ private double real; private double imaginary; public Complex(){ this.real = 0; this.imaginary = 0; } public Complex(double a){ this.real = a; this.imaginary = 0; } public Complex(double a, double b){ this.real = a; this.imaginary = b; } public double getRealPart(){ return this.real; } public double getImaginaryPart(){ return this.imaginary; } public String toString(){ if(this.imaginary==0){ return this.real + ""; } else if(this.real==0){ return this.imaginary + "i"; } else{ return this.real + " + " + this.imaginary + "i"; } } public Complex add(Complex other){ double newReal = this.real + other.getRealPart(); double newImaginary = this.imaginary + other.getImaginaryPart(); return new Complex(newReal, newImaginary); } public Complex subtract(Complex other){ double newReal = this.real - other.getRealPart(); double newImaginary = this.imaginary - other.getImaginaryPart(); return new Complex(newReal, newImaginary); } public Complex multiply(Complex other){ double newReal = this.real * other.getRealPart() - this.imaginary * other.getImaginaryPart(); double newImaginary = this.real * other.getImaginaryPart() + this.imaginary * other.getRealPart(); return new Complex(newReal, newImaginary); } public Complex divide(Complex other){ double denominator = Math.pow(other.getRealPart(),2) + Math.pow(other.getImaginaryPart(),2); double newReal = (this.real * other.getRealPart() + this.imaginary * other.getImaginaryPart()) / denominator; double newImaginary = (this.imaginary * other.getRealPart() - this.real * other.getImaginaryPart()) / denominator; return new Complex(newReal, newImaginary); } public double abs(){ return Math.sqrt(Math.pow(this.real, 2) + Math.pow(this.imaginary, 2)); } public Object clone; public Object clone() throws CloneNotSupportedException{ return super.clone(); }生成这段代码的uml图

#include <iostream> #include <cstring> using namespace std; class CSTRING { public: CSTRING() : ptr(nullptr), len(0) {} CSTRING(const char* str) { len = strlen(str); ptr = new char[len + 1]; strcpy(ptr, str); } CSTRING(const CSTRING& other) { len = other.len; ptr = new char[len + 1]; strcpy(ptr, other.ptr); } ~CSTRING() { if (ptr != nullptr) { delete[] ptr; ptr = nullptr; } } CSTRING& operator=(const CSTRING& rhs) { if (this != &rhs) { if (ptr != nullptr) { delete[] ptr; } len = rhs.len; ptr = new char[len + 1]; strcpy(ptr, rhs.ptr); } return *this; } CSTRING operator+(const CSTRING& rhs) const { CSTRING result; result.len = len + rhs.len; result.ptr = new char[result.len + 1]; strcpy(result.ptr, ptr); strcat(result.ptr, rhs.ptr); return result; } CSTRING& operator+=(const CSTRING& rhs) { len += rhs.len; char* temp = new char[len + 1]; strcpy(temp, ptr); strcat(temp, rhs.ptr); delete[] ptr; ptr = temp; return *this; } char& operator[](int index) { return ptr[index]; } void insert(const char* str, int pos) { int str_len = strlen(str); char* temp = new char[len + str_len + 1]; strncpy(temp, ptr, pos); strcpy(temp + pos, str); strcpy(temp + pos + str_len, ptr + pos); delete[] ptr; ptr = temp; len += str_len; } bool operator>(const CSTRING& rhs) const { return strcmp(ptr, rhs.ptr) > 0; } friend ostream& operator<<(ostream& os, const CSTRING& str); friend istream& operator>>(istream& in, const CSTRING& str); private: char* ptr; int len; }; ostream& operator<<(ostream& os, const CSTRING& str) { os <<"--"<<str.ptr; return os; } istream& operator>>(istream& in, const CSTRING& str) { for (int i = 0; i < str.len ; i++) { cin>>str.ptr[i]; } return in; } int main() { int n, i, j; while (cin >> n) { CSTRING *c = new CSTRING[n + 2]; for (i = 0; i < n; i++) { cin >> c[i]; } for (i = 0; i < n - 1; i++) for (j = 0; j < n - i - 1; j++) if (c[j] > c[j + 1]) { c[n] = c[j]; c[j] = c[j + 1]; c[j + 1] = c[n]; } for (i = 0; i < n; i++) c[n + 1] += c[i]; cout << c[n + 1] << endl; delete[] c; } return 0; }帮我调整代码使其能输出--aaa--bbb--ccc

最新推荐

recommend-type

java中 == 与 equal 的区别讲解

if (this == obj) { return true; } if (obj == null || getClass() != obj.getClass()) { return false; } Student other = (Student) obj; return id == other.id; } @Override public int hashCode() { ...
recommend-type

C语言 if else 语句详细讲解

else printf("This is an other character\n"); return 0; } ``` 在这个例子中,程序根据输入的字符值判断其属于哪一类,并输出相应的信息。 总结起来,`if else`语句是C语言中实现条件控制的重要工具,能够根据...
recommend-type

文本(2024-06-23 161043).txt

文本(2024-06-23 161043).txt
recommend-type

基于单片机的瓦斯监控系统硬件设计.doc

"基于单片机的瓦斯监控系统硬件设计" 在煤矿安全生产中,瓦斯监控系统扮演着至关重要的角色,因为瓦斯是煤矿井下常见的有害气体,高浓度的瓦斯不仅会降低氧气含量,还可能引发爆炸事故。基于单片机的瓦斯监控系统是一种现代化的监测手段,它能够实时监测瓦斯浓度并及时发出预警,保障井下作业人员的生命安全。 本设计主要围绕以下几个关键知识点展开: 1. **单片机技术**:单片机(Microcontroller Unit,MCU)是系统的核心,它集成了CPU、内存、定时器/计数器、I/O接口等多种功能,通过编程实现对整个系统的控制。在瓦斯监控器中,单片机用于采集数据、处理信息、控制报警系统以及与其他模块通信。 2. **瓦斯气体检测**:系统采用了气敏传感器来检测瓦斯气体的浓度。气敏传感器是一种对特定气体敏感的元件,它可以将气体浓度转换为电信号,供单片机处理。在本设计中,选择合适的气敏传感器至关重要,因为它直接影响到检测的精度和响应速度。 3. **模块化设计**:为了便于系统维护和升级,单片机被设计成模块化结构。每个功能模块(如传感器接口、报警系统、电源管理等)都独立运行,通过单片机进行协调。这种设计使得系统更具有灵活性和扩展性。 4. **报警系统**:当瓦斯浓度达到预设的危险值时,系统会自动触发报警装置,通常包括声音和灯光信号,以提醒井下工作人员迅速撤离。报警阈值可根据实际需求进行设置,并且系统应具有一定的防误报能力。 5. **便携性和安全性**:考虑到井下环境,系统设计需要注重便携性,体积小巧,易于携带。同时,系统的外壳和内部电路设计必须符合矿井的安全标准,能抵抗井下潮湿、高温和电磁干扰。 6. **用户交互**:系统提供了灵敏度调节和检测强度调节功能,使得操作员可以根据井下环境变化进行参数调整,确保监控的准确性和可靠性。 7. **电源管理**:由于井下电源条件有限,瓦斯监控系统需具备高效的电源管理,可能包括电池供电和节能模式,确保系统长时间稳定工作。 通过以上设计,基于单片机的瓦斯监控系统实现了对井下瓦斯浓度的实时监测和智能报警,提升了煤矿安全生产的自动化水平。在实际应用中,还需要结合软件部分,例如数据采集、存储和传输,以实现远程监控和数据分析,进一步提高系统的综合性能。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

:Python环境变量配置从入门到精通:Win10系统下Python环境变量配置完全手册

![:Python环境变量配置从入门到精通:Win10系统下Python环境变量配置完全手册](https://img-blog.csdnimg.cn/20190105170857127.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI3Mjc2OTUx,size_16,color_FFFFFF,t_70) # 1. Python环境变量简介** Python环境变量是存储在操作系统中的特殊变量,用于配置Python解释器和
recommend-type

electron桌面壁纸功能

Electron是一个开源框架,用于构建跨平台的桌面应用程序,它基于Chromium浏览器引擎和Node.js运行时。在Electron中,你可以很容易地处理桌面环境的各个方面,包括设置壁纸。为了实现桌面壁纸的功能,你可以利用Electron提供的API,如`BrowserWindow` API,它允许你在窗口上设置背景图片。 以下是一个简单的步骤概述: 1. 导入必要的模块: ```javascript const { app, BrowserWindow } = require('electron'); ``` 2. 在窗口初始化时设置壁纸: ```javas
recommend-type

基于单片机的流量检测系统的设计_机电一体化毕业设计.doc

"基于单片机的流量检测系统设计文档主要涵盖了从系统设计背景、硬件电路设计、软件设计到实际的焊接与调试等全过程。该系统利用单片机技术,结合流量传感器,实现对流体流量的精确测量,尤其适用于工业过程控制中的气体流量检测。" 1. **流量检测系统背景** 流量是指单位时间内流过某一截面的流体体积或质量,分为瞬时流量(体积流量或质量流量)和累积流量。流量测量在热电、石化、食品等多个领域至关重要,是过程控制四大参数之一,对确保生产效率和安全性起到关键作用。自托里拆利的差压式流量计以来,流量测量技术不断发展,18、19世纪出现了多种流量测量仪表的初步形态。 2. **硬件电路设计** - **总体方案设计**:系统以单片机为核心,配合流量传感器,设计显示单元和报警单元,构建一个完整的流量检测与监控系统。 - **工作原理**:单片机接收来自流量传感器的脉冲信号,处理后转化为流体流量数据,同时监测气体的压力和温度等参数。 - **单元电路设计** - **单片机最小系统**:提供系统运行所需的电源、时钟和复位电路。 - **显示单元**:负责将处理后的数据以可视化方式展示,可能采用液晶显示屏或七段数码管等。 - **流量传感器**:如涡街流量传感器或电磁流量传感器,用于捕捉流量变化并转换为电信号。 - **总体电路**:整合所有单元电路,形成完整的硬件设计方案。 3. **软件设计** - **软件端口定义**:分配单片机的输入/输出端口,用于与硬件交互。 - **程序流程**:包括主程序、显示程序和报警程序,通过流程图详细描述了每个程序的执行逻辑。 - **软件调试**:通过调试工具和方法确保程序的正确性和稳定性。 4. **硬件电路焊接与调试** - **焊接方法与注意事项**:强调焊接技巧和安全事项,确保电路连接的可靠性。 - **电路焊接与装配**:详细步骤指导如何组装电路板和连接各个部件。 - **电路调试**:使用仪器设备检查电路性能,排除故障,验证系统功能。 5. **系统应用与意义** 随着技术进步,单片机技术、传感器技术和微电子技术的结合使得流量检测系统具备更高的精度和可靠性,对于优化工业生产过程、节约资源和提升经济效益有着显著作用。 6. **结论与致谢** 文档结尾部分总结了设计成果,对参与项目的人表示感谢,并可能列出参考文献以供进一步研究。 7. **附录** 包含程序清单和电路总图,提供了具体实现细节和设计蓝图。 此设计文档为一个完整的机电一体化毕业设计项目,详细介绍了基于单片机的流量检测系统从概念到实施的全过程,对于学习单片机应用和流量测量技术的读者具有很高的参考价值。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

:Python环境变量配置实战:Win10系统下Python环境变量配置详解

![python配置环境变量win10](https://img-blog.csdnimg.cn/20190105170857127.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI3Mjc2OTUx,size_16,color_FFFFFF,t_70) # 1. Python环境变量配置概述 环境变量是计算机系统中存储和管理配置信息的特殊变量。在Python中,环境变量用于指定Python解释器和库的安装路径,以及其他影响