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;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)