3.5 equals()

3.5 Equality in ADT and OOP
等价性equals()和==
1、== 引用等价性
  • == 对基本数据类型,使用==判定相等
  • 如果用==,是在判断两个对象身份标识 ID是否相等(指向内存里的同一段空间)
2、equals() 对象等价性
  • 在自定义ADT时,需要重写Object的equals()
  • 对对象类型,使用equals()

Equality of immutable types 的重写override 和 重载overload
首先,在Object中实现的缺省equals()是在判断引用等价性,通常不是程序员所期望的
所以需要重写,有两种方式
1、overload
直接使用这个equal()来实现下面的代码会出错
这样进行改进
2、Override

参数类型仍未Object,所以这是override
意义在于 先判断thatObject是否为Duration的类型,然后再将其转成Duration的引用,再将转换后的引用对象调用方法和其进行比较。

equals()的自反、传递、对称

hashCode()
1、两个被认为equal的对象hashcode应该相同;
override equal()必须重写hashcode
2、unequal的对象hashcode不能相同
3、Hash code must not change unless object mutated
继续以上例为例

可变对象的观察等价性、行为等价性
观察等价性:在不改变状态的情况下,两个mutable对象是否看起来一致
行为等价性:调用对象的任何方法都展示出一致的结果

对可变类型,实现行为等价性即可。也就是说,只有指向同样内存空间的objects,才是相等的。
所以对可变类型来说,无需重写这两个函数,直接继承Object对象的两个方法即可。
如果一定要判断两个可变对象看起来是否一致,最好定义一个新的方法。

可变不可变对equals()和hashcode()的比较
不可变
可变
equals()实现行为等价性
equals()在实现引用等价性==的基础上实现行为等价性
hashcode()把abstrract value映射成一个整数
hashcode()把引用映射成一个整数
equals()改变必须重写hashcode()
equals()改变无需重写hashcode()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 以下是实现用户输入两个数字(二十以内的整数和小数)的英文单词,计算出二者乘积,并输出结果数字及结果的英文单词的C++代码: ```c++ #include <iostream> #include <string> using namespace std; string nums[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty"}; int getNum(string s) { for (int i = 0; i <= 20; i++) { if (s == nums[i]) { return i; } } return -1; } int main() { string num1, num2; while (true) { cout << "Please enter two numbers (within 20): "; cin >> num1 >> num2; int int1 = getNum(num1); int int2 = getNum(num2); if (int1 == -1 || int2 == -1) { cout << "Invalid input, please try again." << endl; continue; } double product = int1 * stod(num2); int intProduct = (int)product; cout << nums[int1] << " times " << num2 << " equals " << intProduct << " (" << nums[intProduct] << ")" << endl; } return 0; } ``` 运行效果如下: ``` Please enter two numbers (within 20): five 3.5 five times 3.5 equals 17 (seventeen) Please enter two numbers (within 20): eleven 2.7 eleven times 2.7 equals 29 (twenty-nine) Please enter two numbers (within 20): thirty six Invalid input, please try again. Please enter two numbers (within 20): fourteen twenty two Invalid input, please try again. Please enter two numbers (within 20):``` ### 回答2: 下面是一个用C++编写的程序,实现用户输入两个数字(二十以内的整数和小数)的英文单词,计算出二者乘积,并输出结果数字及结果的英文单词。程序使用了string类完成,可以完成多次计算。 ```cpp #include <iostream> #include <string> using namespace std; string numberToWord(int num) { string words = ""; string ones[] = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; string teens[] = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}; string tens[] = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}; if (num == 0) { return "zero"; } else if (num < 10) { words += ones[num]; } else if (num < 20) { words += teens[num - 10]; } else if (num < 100) { words += tens[num / 10]; if (num % 10 != 0) { words += " " + ones[num % 10]; } } return words; } int main() { while (true) { cout << "请输入第一个数字(二十以内的整数或小数):" << endl; double num1; cin >> num1; cout << "请输入第二个数字(二十以内的整数或小数):" << endl; double num2; cin >> num2; double result = num1 * num2; string resultWord = numberToWord(result); cout << "结果:" << result << "(" << resultWord << ")" << endl; cout << "是否继续计算?(Y/N)" << endl; char choice; cin >> choice; if (choice != 'Y' && choice != 'y') { break; } cout << endl; } return 0; } ``` 程序运行时,首先会提示用户输入第一个数字(二十以内的整数或小数),然后提示用户输入第二个数字(二十以内的整数或小数)。接下来程序会计算两个数字的乘积,并将结果以数字及其英文单词的形式输出。程序运行结束后,会询问用户是否继续计算,若用户输入"Y"或"y",则可以继续输入两个数字进行计算,否则程序结束。 ### 回答3: 以下是一个使用string类完成用户输入两个数字,并计算乘积并输出结果的C++程序代码: ```c++ #include <iostream> #include <string> std::string toEnglishWord(int num) { // 数字对应的英文单词数组 std::string words[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; // 将数字转换成英文单词,如0转为"zero" return words[num]; } std::string toEnglishWord(double num) { // 判断小数部分是否为0 if (num - (int)num == 0) { return toEnglishWord((int)num); } // 转换整数部分 std::string integerPart = toEnglishWord((int)num); // 转换小数部分 std::string decimalPart; std::string decimalNum = std::to_string(num); size_t pos = decimalNum.find("."); decimalNum = decimalNum.substr(pos + 1); for (char digit : decimalNum) { decimalPart += toEnglishWord(digit - '0') + " "; } return integerPart + " point " + decimalPart; } int main() { while (true) { std::cout << "请输入两个数字(二十以内的整数和小数):" << std::endl; int num1; std::cin >> num1; std::string word1 = toEnglishWord(num1); double num2; std::cin >> num2; std::string word2 = toEnglishWord(num2); double result = num1 * num2; std::string resultWord = toEnglishWord(result); std::cout << "结果数字及结果的英文单词:" << std::endl; std::cout << "数字1:" << num1 << ",英文单词:" << word1 << std::endl; std::cout << "数字2:" << num2 << ",英文单词:" << word2 << std::endl; std::cout << "结果:" << result << ",英文单词:" << resultWord << std::endl; std::cout << std::endl; } return 0; } ``` 用户在控制台中输入两个数字(一个整数和一个小数),程序将会计算两个数字的乘积,并将结果的数字及结果的英文单词输出在控制台中。用户可以多次输入不同的数字进行计算。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值