鱼C论坛

 找回密码
 立即注册
查看: 780|回复: 2

[已解决]java重写equals

[复制链接]
发表于 2023-10-27 20:41:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
构造手机类,包含其配置信息:型号(字符串)、内存大小(整数)、存储空间(整数,GB为单位)、价格(整数)。提供带参数的构造函数,重写其equals方法,使得两个相同配置(型号、内存、存储相同即可,价格可不同)的手机为相等的手机。重写其toString函数,打印手机的配置信息,形式为CellPhone [model:xxx, memory:xxx, storage:xxx, price:xxx]
main函数中从键盘读入两个手机对象,比较他们是否相等,输出他们的配置信息。

输入描述:
两个计算机对象,包含型号、内存、存储空间、价格

输出描述:
两个对象是否相等,两个对象的配置信息
输入样例:
在这里给出一组输入。例如:

P20 8 64 4999
P20 8 64 4999
输出样例:
在这里给出相应的输出。例如:

true
CellPhone [model:P20, memory:8, storage:64, price:4999]
CellPhone [model:P20, memory:8, storage:64, price:4999]
下面是我写的代码,重写equals那个有点问题,望指正。
  1. import java.util.Objects;
  2. import java.util.Scanner;
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         Scanner sc = new Scanner(System.in);
  6.         CellPhone c1 = new CellPhone(sc.next(),sc.nextInt(),sc.nextInt(),sc.nextInt());
  7.         CellPhone c2 = new CellPhone(sc.next(),sc.nextInt(),sc.nextInt(),sc.nextInt());

  8.         System.out.println(c1.equals(c2));
  9.         System.out.println(c1);
  10.         System.out.println(c2);

  11.     }
  12. }

  13. /* 你的代码将被嵌在这里 */
  14. class CellPhone{
  15.     String a;
  16.     int b;
  17.     int c;
  18.     int d;
  19.     public CellPhone(String a,int b,int c,int d){
  20.         this.a=a;
  21.         this.b=b;
  22.         this.c=c;
  23.         this.d=d;
  24.     }
  25.     @Override
  26.     public String toString(){
  27.         return "CellPhone [model:"+a+", memory:"+b+", storage:"+c+", price:"+d+"]";
  28.     }
  29.     @Override
  30.     public boolean equals(Object obj){
  31.         CellPhone c=(CellPhone) obj;
  32.         if(this==c) {
  33.             return true;
  34.         }
  35.         return false;
  36.     }
  37. }
复制代码
最佳答案
2023-10-27 20:42:00
重写equals方法时,需要比较两个对象的型号、内存和存储空间是否相同。你的代码中,只是简单地比较了两个对象的引用是否相同,这是不正确的。下面是修改后的代码:

  1. import java.util.Objects;
  2. import java.util.Scanner;

  3. public class Main {
  4.     public static void main(String[] args) {
  5.         Scanner sc = new Scanner(System.in);
  6.         CellPhone c1 = new CellPhone(sc.next(), sc.nextInt(), sc.nextInt(), sc.nextInt());
  7.         CellPhone c2 = new CellPhone(sc.next(), sc.nextInt(), sc.nextInt(), sc.nextInt());

  8.         System.out.println(c1.equals(c2));
  9.         System.out.println(c1);
  10.         System.out.println(c2);
  11.     }
  12. }

  13. class CellPhone {
  14.     String model;
  15.     int memory;
  16.     int storage;
  17.     int price;

  18.     public CellPhone(String model, int memory, int storage, int price) {
  19.         this.model = model;
  20.         this.memory = memory;
  21.         this.storage = storage;
  22.         this.price = price;
  23.     }

  24.     @Override
  25.     public String toString() {
  26.         return "CellPhone [model:" + model + ", memory:" + memory + ", storage:" + storage + ", price:" + price + "]";
  27.     }

  28.     @Override
  29.     public boolean equals(Object obj) {
  30.         if (this == obj) {
  31.             return true;
  32.         }
  33.         if (obj == null || getClass() != obj.getClass()) {
  34.             return false;
  35.         }
  36.         CellPhone other = (CellPhone) obj;
  37.         return Objects.equals(model, other.model) &&
  38.                 memory == other.memory &&
  39.                 storage == other.storage;
  40.     }
  41. }
复制代码


在重写equals方法中,首先判断两个对象的引用是否相同,如果相同则返回true。然后判断传入的对象是否为null或者不是CellPhone类的实例,如果是则返回false。最后比较两个对象的型号、内存和存储空间是否相同,使用Objects.equals方法来比较字符串的相等性,使用==运算符来比较整数的相等性。如果都相等,则返回true,否则返回false。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-10-27 20:42:00 | 显示全部楼层    本楼为最佳答案   
重写equals方法时,需要比较两个对象的型号、内存和存储空间是否相同。你的代码中,只是简单地比较了两个对象的引用是否相同,这是不正确的。下面是修改后的代码:

  1. import java.util.Objects;
  2. import java.util.Scanner;

  3. public class Main {
  4.     public static void main(String[] args) {
  5.         Scanner sc = new Scanner(System.in);
  6.         CellPhone c1 = new CellPhone(sc.next(), sc.nextInt(), sc.nextInt(), sc.nextInt());
  7.         CellPhone c2 = new CellPhone(sc.next(), sc.nextInt(), sc.nextInt(), sc.nextInt());

  8.         System.out.println(c1.equals(c2));
  9.         System.out.println(c1);
  10.         System.out.println(c2);
  11.     }
  12. }

  13. class CellPhone {
  14.     String model;
  15.     int memory;
  16.     int storage;
  17.     int price;

  18.     public CellPhone(String model, int memory, int storage, int price) {
  19.         this.model = model;
  20.         this.memory = memory;
  21.         this.storage = storage;
  22.         this.price = price;
  23.     }

  24.     @Override
  25.     public String toString() {
  26.         return "CellPhone [model:" + model + ", memory:" + memory + ", storage:" + storage + ", price:" + price + "]";
  27.     }

  28.     @Override
  29.     public boolean equals(Object obj) {
  30.         if (this == obj) {
  31.             return true;
  32.         }
  33.         if (obj == null || getClass() != obj.getClass()) {
  34.             return false;
  35.         }
  36.         CellPhone other = (CellPhone) obj;
  37.         return Objects.equals(model, other.model) &&
  38.                 memory == other.memory &&
  39.                 storage == other.storage;
  40.     }
  41. }
复制代码


在重写equals方法中,首先判断两个对象的引用是否相同,如果相同则返回true。然后判断传入的对象是否为null或者不是CellPhone类的实例,如果是则返回false。最后比较两个对象的型号、内存和存储空间是否相同,使用Objects.equals方法来比较字符串的相等性,使用==运算符来比较整数的相等性。如果都相等,则返回true,否则返回false。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-27 20:43:10 | 显示全部楼层
你的代码中,equals方法的实现有一点问题。在equals方法中,应该比较两个对象的型号(a)、内存大小(b)和存储空间(c)是否相等,如果相等则返回true,否则返回false。以下是修改后的代码:

[/code]java
import java.util.Objects;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        CellPhone c1 = new CellPhone(sc.next(), sc.nextInt(), sc.nextInt(), sc.nextInt());
        CellPhone c2 = new CellPhone(sc.next(), sc.nextInt(), sc.nextInt(), sc.nextInt());
        
        System.out.println(c1.equals(c2));
        System.out.println(c1);
        System.out.println(c2);
    }
}

class CellPhone {
    String model;
    int memory;
    int storage;
    int price;
   
    public CellPhone(String model, int memory, int storage, int price) {
        this.model = model;
        this.memory = memory;
        this.storage = storage;
        this.price = price;
    }
   
    @Override
    public String toString() {
        return "CellPhone [model: " + model + ", memory: " + memory + ", storage: " + storage + ", price: " + price + "]";
    }
   
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        
        CellPhone other = (CellPhone) obj;
        return Objects.equals(model, other.model)
                && memory == other.memory
                && storage == other.storage;
    }
}
[/code]

这样修改后,equals方法会比较两个手机对象的型号、内存大小和存储空间是否相等,如果相等则返回true,否则返回false。在toString方法中,打印出手机的配置信息。

请注意保持代码的格式整齐,并确保输入输出的格式与题目描述一致。

球一个最佳答案谢谢啦!这对我非常重要!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-12 10:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表