在主要方法中使用可比较的方法

发布于 2025-01-22 20:42:02 字数 713 浏览 1 评论 0原文

我正在尝试检查一个可比较的方法的值,并将1和-1值分配给True和False。我做了一个简单的话,如果其他陈述可以做到这一点,但是我想在方法内部完成它,以便我可以在主要方法中多次使用它。当我尝试执行此操作时,我会遇到一个错误,即我的比较方法(在另一个类中)是“针对类型对象的未定义”。

这是我的比较方法的代码,也是我在测试类中使用它的尝试。

public int compareTo(FPNumber o) {
    if ((exp == o.exp) && (fraction - o.fraction < SMALL))
        return 1;
    else if ((exp == o.exp) || (fraction - o.fraction > SMALL))
        return -1;
    else
        return 0;
}
public String compare(Object FP1, Object FP2) {
    if (FP1.compareTo(FP2) == 1)
        System.out.println("true");
    else if (FP1.compareTo(FP2) == -1)
        System.out.println("false");
    else
        System.out.println("error");
}

I am trying to check the values of a comparable method and assign the 1 and -1 value to true and false. I made a simple if else statement that does just that but I want to make it inside of a method so I can use it multiple times in my main method. When I try to do this I get an error that my compareTo method (in another class) is "undefined for the type Object".

Here is my code for both the compareTo method and my attempt of using this in my test class.

public int compareTo(FPNumber o) {
    if ((exp == o.exp) && (fraction - o.fraction < SMALL))
        return 1;
    else if ((exp == o.exp) || (fraction - o.fraction > SMALL))
        return -1;
    else
        return 0;
}
public String compare(Object FP1, Object FP2) {
    if (FP1.compareTo(FP2) == 1)
        System.out.println("true");
    else if (FP1.compareTo(FP2) == -1)
        System.out.println("false");
    else
        System.out.println("error");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

任性一次 2025-01-29 20:42:02

让我以一个简单的示例使用原始值开始,然后将其展开以使用对象。

假设您有两个变量xy容纳整数值。如果我问您,您怎么知道这些变量的值是否相等?问题通过简单的数学回答:如果两个变量的值相等,则两个变量的差异必须为零。例如,5-5。在这种情况下,差异为零,因为两个变量都具有正值5。

如果它们不同,该怎么办?令x = 5y = 13

  1. x -y = -8(这意味着x&lt; y
  2. y -x = 8(与上述相同)

,当值不同时,并不总是会被h。 1或-1。当您比较两个以上的值时,这一点很重要。让我们介绍z = 20。如果将xyxz进行比较,则结果为-1 yz必须是相等的,但事实并非如此。

比较对象时怎么样?这是相同的原则。即使对象持有多个变量,您也必须决定一个层次结构,以确定哪个变量或多或少在比较中很重要。考虑以下示例,

public class Person implements Comparable<Person> {
    private String firstName;
    private String lastName;
    private int age;
    ...
    public int compareTo(Person other) {...}
}

我可以决定,对于我的系统,在比较两个person对象时,我必须先检查姓氏,然后是名字,最后是年龄。

public int compareTo(Person other) {
    int i = lastName.compareTo(other.lastName);
    if (i != 0) return i;

    i = firstName.compareTo(other.firstName);
    if (i != 0) return i;

    return Integer.compare(age, other.age);
}

基本上,如果姓氏相同(i == 0),则将比较名字。然后,如果名字相同,它将比较年龄。在任何时候,如果值不同,则将返回差异。同样,不是1或-1。因此,基本上,要将结果转换为boolean,逻辑是

public boolean compare(Person person, Person other) {
    if (person.compareTo(other) == 0) return true;
    else return false;
}

顺便说一句,您的原始代码具有汇编错误,因为您的比较方法应返回string,然后返回代码> void 。而不是像现在一样在方法内使用system.ut.print(),而是应该打印出该方法的输出。

public String compare(Object FP1, Object FP2) {
        if (FP1.compareTo(FP2) == 1)
            return "true";
        else if (FP1.compareTo(FP2) == -1)
            return "false";
        else
            return "error";
}

...

System.out.println(compare(FP1, FP2));

update :我忘了在此之前提及,从本质上讲,比较函数我在此处包括的功能基本上与equals()方法基本相同。另外,由于此功能由第三方提供,所以它是比较器应该做的。因此,应该按照比较器做什么的最佳实践来完成。对于我的Person比较器,您可以有两个比较器:一个比较年龄和比较名称的比较。

Let me start by a simple example using raw values, and then expand it to use objects.

Suppose you have two variables x and y that hold integer values. If I ask you, how do you know if the values for these variables are equal? The question is answered by simple math: if the values of the two variables are equal, the difference between the two must be zero. For example, 5 - 5. In this case, the difference is zero because both variables hold the value of positive 5.

What if they are different? Let x = 5 and y = 13.

  1. x - y = -8 (this means that x < y)
  2. y - x = 8 (same as above)

As you can see, when the values are different, it is not always going to be 1 or -1. This is important when you are comparing more than two values. Let's introduce z = 20. If comparing x to y and x to z and the result was -1 on both comparisons, the implication is that y and z must be equal but they are not.

What about when comparing objects? It is the same principle. Even when an object holds multiple variables, you must decide a hierarchy to determine which variable is more or less important in the comparison. Consider the following example

public class Person implements Comparable<Person> {
    private String firstName;
    private String lastName;
    private int age;
    ...
    public int compareTo(Person other) {...}
}

I can decide that, for my system, when comparing two Person objects, I must check first the last name, then the first name, and lastly the age.

public int compareTo(Person other) {
    int i = lastName.compareTo(other.lastName);
    if (i != 0) return i;

    i = firstName.compareTo(other.firstName);
    if (i != 0) return i;

    return Integer.compare(age, other.age);
}

Basically, if the last names are the same (i == 0), it will compare the first names. Then if the first names are the same, it will compare the ages. At any point, if the values are different, the difference will be returned. Again, not a 1 or -1. So basically, to convert the results to boolean, the logic is

public boolean compare(Person person, Person other) {
    if (person.compareTo(other) == 0) return true;
    else return false;
}

By the way, your original code has a compilation error because your compare method should return a String and it returns void. Instead of using System.out.print() inside your method, like you have now, you should print out the output of the method.

public String compare(Object FP1, Object FP2) {
        if (FP1.compareTo(FP2) == 1)
            return "true";
        else if (FP1.compareTo(FP2) == -1)
            return "false";
        else
            return "error";
}

...

System.out.println(compare(FP1, FP2));

UPDATE: I forgot to mention before that, essentially, the compare function I included here is serving basically the same function as the equals() method. Also, because this function is provided by a third party, it is sort of what a Comparator should do. Because of that, it should be done following best practices of what a Comparator should do. For my Person comparator, you may have two Comparators: one that compares age and one that compare names.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文