java Java中的“其他”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30277232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:48:58  来源:igfitidea点击:

what does "other" mean in Java?

javaobjectinterfacecomparable

提问by Fuzz X

I have this code, but I fail to understand what "other" actually is, and what it's trying to do.

我有这段代码,但我不明白“其他”实际上是什么,以及它试图做什么。

public interface Comparable<T>
{
    int compareTo(T other);
}

What does the parameter "other" suppose to mean?

参数“other”是什么意思?

回答by Jon Skeet

It's just a parameter name, not a keyword. It's the other value you're comparing "this" value to. So suppose you're comparing two people, you might have:

它只是一个参数名称,而不是关键字。这是您要与“此”值进行比较的另一个值。因此,假设您要比较两个人,您可能有:

Person fred = new Person(...);
Person george = new Person(...);
int result = fred.compareTo(george);

It would be up to the compareTomethod to compare fredand george(which it would know as thisand other) by whatever means it deemed appropriate, e.g. age, name etc.

这将是最多的compareTo方法来比较fredgeorge(它会知道的thisother)由任何它认为适当的,如年龄,姓名等。

回答by CupawnTae

It's not a keyword, it's a parameter name that indicates the purpose of the parameter. An implementation of the interface can call the parameter the same thing or use a different name, so really in an interface it's just an FYI.

它不是关键字,而是指示参数用途的参数名称。接口的实现可以调用相同的参数或使用不同的名称,因此实际上在接口中它只是一个仅供参考。

In this case it's for comparing an object with anotherobject, so that parameter is the otherto compare against.

在这种情况下,它用于将一个对象与另一个对象进行比较,因此该参数是另一个要比较的参数。

回答by lethanh

It's a parameter with the type is T, don't be confused with meaning of the word

它是一个类型为 T 的参数,不要与单词的含义混淆

回答by HyperNeutrino

The word otheris not a keyword; rather, it is the name of a parameter. In Java, methods take parameters, which have the following syntax:

这个词other不是关键字;相反,它是参数的名称。在 Java 中,方法 take parameters,其语法如下:

[final] Type name

So a method could look like this:

所以一个方法可能是这样的:

int compareTo(Object other) {}

where the method returns an int, and takes one argument of type Objectcalled other. Inside the method, you can refer to the Objectother. To call this method, you would use the following syntax:

其中该方法返回一个int,并采用一个Object名为的类型参数other。在方法内部,您可以参考Objectother. 要调用此方法,您将使用以下语法:

compareTo(new Object());

A parameter, like local variables and fields, can be final, but not transientor volatile.

参数,如局部变量和字段,可以是final,但不能是transientvolatile

回答by SMA

It's not a keyword but a parameter passed to compareTo method. It just means that another object of type T which will be compared with object that invoked compareTomethod.

它不是关键字,而是传递给 compareTo 方法的参数。它只是意味着另一个 T 类型的对象将与调用compareTo方法的对象进行比较。

So for Example, if you have class Person, with id, name and you want to compare two person with id, you could do something like:

因此,例如,如果您有一个 Person 类,带有 id,name 并且您想比较两个带有 id 的人,您可以执行以下操作:

 Person person1 = new Person(1, "abc");//Assume Person implements Comparable
 Person person2 = new Person(2, "pqr");
 person1.compareTo(person2);

So here person2 is the other object that you are referring to.

所以这里的 person2 是你所指的另一个对象。