如何重写equalsignorecase方法

   2024-10-08 9540
核心提示:要重写equalsIgnoreCase方法,首先需要创建一个新的方法,而不是重写String类中的equalsIgnoreCase方法。以下是一个示例:public

要重写equalsIgnoreCase方法,首先需要创建一个新的方法,而不是重写String类中的equalsIgnoreCase方法。以下是一个示例:

public class CustomString {    private String value;    public CustomString(String value) {        this.value = value;    }    @Override    public boolean equals(Object obj) {        if (obj instanceof CustomString) {            CustomString other = (CustomString) obj;            return this.value.equalsIgnoreCase(other.value);        }        return false;    }    public static void main(String[] args) {        CustomString str1 = new CustomString("Hello");        CustomString str2 = new CustomString("hello");        System.out.println(str1.equals(str2)); // Output: true    }}

在上面的例子中,我们创建了一个自定义的CustomString类,并重写了equals方法,以便忽略大小写比较两个CustomString对象的值。在main方法中,我们创建了两个CustomString对象,并调用equals方法进行比较。最终输出结果为true,表示两个对象的值在忽略大小写的情况下是相等的。

 
举报打赏
 
更多>同类维修大全
推荐图文
推荐维修大全
点击排行

网站首页  |  关于我们  |  联系方式网站留言    |  赣ICP备2021007278号