Java Can';不使用方法?

Java Can';不使用方法?,java,generics,Java,Generics,我有一个任务来实现一个通用的Pair接口,它表示有序的Pair,比如(x,y)。我试图编写equals方法,但是我必须将参数设置为一个普通对象,而不是一对,所以我不知道如何获取它的坐标。当我试图编译时,我得到一个symbol not found错误,因为对象类没有fst和snd方法。我应该抛出异常还是什么?注意:我的教授给了我们一个模板,我只是在填写方法,我认为我不允许更改参数或方法 以下是相关代码: public class Pair<T1,T2> implements P

我有一个任务来实现一个通用的Pair接口,它表示有序的Pair,比如(x,y)。我试图编写equals方法,但是我必须将参数设置为一个普通对象,而不是一对,所以我不知道如何获取它的坐标。当我试图编译时,我得到一个symbol not found错误,因为对象类没有fst和snd方法。我应该抛出异常还是什么?注意:我的教授给了我们一个模板,我只是在填写方法,我认为我不允许更改参数或方法

以下是相关代码:

    public class Pair<T1,T2> implements PairInterface<T1,T2>
{
private T1 x;
private T2 y;

public Pair(T1 aFirst, T2 aSecond)
{
    x = aFirst;
    y = aSecond;
}

/**
 * Gets the first element of this pair.
 * @return the first element of this pair.
 */
public T1 fst()
{
    return x;
}

/**
 * Gets the second element of this pair.
 * @return the second element of this pair.
 */
public T2 snd()
{
    return y;
}
以下是我得到的错误:

    ./Pair.java:66: cannot find symbol
    symbol  : method fst()
    location: class java.lang.Object
    T1 a = otherObject.fst();
                      ^
    ./Pair.java:67: cannot find symbol
    symbol  : method snd()
    location: class java.lang.Object
    T2 b = otherObject.snd();
                      ^

类比较后
if(getClass()!=otherObject.getClass())返回false

您需要创建具有适当类型的新变量,如下所示:

Pair<T1,T2> pair = (Pair<T1,T2>)otherObject;
Pair-Pair=(Pair)otherObject;

然后,您将能够使用
pair
及其方法

在调用其他对象的方法之前将其强制转换为pair

Pair<T1, T2> otherPair = (Pair) otherObject;
T1 a = otherPair.fst();
T2 b = otherPair.snd();
Pair otherPair=(Pair)otherObject;
T1 a=otherPair.fst();
t2b=otherPair.snd();

这是因为您没有首先强制转换其他对象,所以JVM认为它属于Object类型。对象没有方法fst()和snd(),因此会失败

要解决此问题,请将otherObject强制转换为Pair,然后调用fst()和snd():

公共布尔等于(对象其他对象)
{
if(otherObject==null)
{
返回false;
}
如果(getClass()!=otherObject.getClass())
{
返回false;
}
T1 a=((对)otherObject.fst();
t2b=((对)其他对象).snd();
如果(x等于(a)和&y等于(b))
{
返回true;
}
其他的
{
返回false;
}
}

方法的
equals
参数是一个
对象
,它不保证有您的方法
fst
snd
,因此编译器错误。为了能够调用这些方法,您需要有一个
对象

标准做法是测试传入对象的类,查看它是否与
this
是同一类,如果不是同一类,则返回
false
。通常这是通过
instanceof
完成的,如果
为true
,则执行强制转换。强制转换允许编译器将对象视为您所说的类。这允许编译器找到要调用的方法

if(otherObject == null)
{
    return false;
}

// Use instanceof
if(!(otherObject instanceof Pair))
{
    return false;
}
// Cast - use <?, ?>; we don't know what they are.
Pair<?, ?> otherPair = (Pair<?, ?>) otherObject;
Object a = otherPair.fst();
Object b = otherPair.snd();
// Simplified return
return (x.equals(a) && y.equals(b));
if(otherObject==null)
{
返回false;
}
//使用instanceof
如果(!(其他对象实例对))
{
返回false;
}
//铸造使用;我们不知道它们是什么。
Pair otherPair=(Pair)otherObject;
对象a=otherPair.fst();
对象b=otherPair.snd();
//简化回报
回报率(x等于(a)和y等于(b));

在确保
getClass()!=如果otherObject.getClass()
为true,则可以对
Pair-Pair=(Pair)otherObject执行安全强制转换。然后,按原样使用代码注意:您无法对
对执行安全强制转换,因为您只知道其他对象是
对的实例。但是,您与
.equals()
的比较应该仍然有效。

您必须先将另一个对象显式转换为成对对象(考虑到您已经知道它是一个
成对对象,因为类是相同的),但您不知道它的类型,因此应该使用通配符

Pair<?, ?> otherPair = (Pair<?, ?>)otherObject;
//add null-checks if fst() or snd() is nullable
if(this.fst().equals(otherPair.fst()) && this.snd().equals(otherPair.snd()) {
    return true;
} else {
    return false;
}
Pair otherPair=(Pair)otherObject;
//添加null检查fst()或snd()是否可为null
if(this.fst().equals(otherPair.fst())&&this.snd().equals(otherPair.snd()){
返回true;
}否则{
返回false;
}

您可以将任何对象类型传递给equals(),因此正确实现equals的一部分是检查参数对象的类型

如果查看java核心类,它们通常首先检查==然后检查它是否是实现equals()的类的实例

一般来说,您应该检查: 1) ==相等,即参数是否是对当前对象本身的引用-如果是,则每个equals()的对象应相等。这在计算上很便宜。 2) 参数是否为空。这在计算上也很便宜,如果为true,则确保equals()返回false。 3) 参数对象是否为当前对象类的实例。如果不是,就不能平等,演员阵容无论如何都会失败。 4) 然后铸造它,并开始相互比较零件


懒惰程序员提示-eclipse可以使用向导自动生成漂亮的java equals()块。在右键单击>源代码>生成hashcode()equals()和bam中,它生成一个适当的equals()方法

请阅读有关在中强制转换的部分。坦率地说,我很惊讶你的老师让你使用泛型,如果你还不知道如何转换,这是一件相当复杂的事情。此外,如果您使用
first
second
而不是
fst
snd
,那么代码的可读性将非常高。一旦您建立了实际的对象类型,您将需要执行显式强制转换。您必须将另一个对象强制转换为一对,首先
pair otherPair=(pair)otherObject。你真的能做
,还是他们应该使用
?我现在正在考虑这个问题,我不确定。@EpicPandaForce你可以同时使用这两种方法,我更喜欢将构造转换为T1,t2,如果(foo)返回true;否则返回false
可以简化为
返回foo@bcsb1001是的,我只是觉得乍一看这更干净。我想你是说!(对的其他对象实例)返回false?
getClass()
方法与此方法的区别在于此方法允许子类。“如果不是同一类,则返回false”的语句不正确。请注意这一点。注意:您可以在运行时执行安全强制转换成对,因为它们实际上只是Object@Lashane在运行时,它实际上是
。而且,尽管这种演员阵容是合法的,但这是一种糟糕的做法。可能吧
if(otherObject == null)
{
    return false;
}

// Use instanceof
if(!(otherObject instanceof Pair))
{
    return false;
}
// Cast - use <?, ?>; we don't know what they are.
Pair<?, ?> otherPair = (Pair<?, ?>) otherObject;
Object a = otherPair.fst();
Object b = otherPair.snd();
// Simplified return
return (x.equals(a) && y.equals(b));
Pair<?, ?> otherPair = (Pair<?, ?>)otherObject;
//add null-checks if fst() or snd() is nullable
if(this.fst().equals(otherPair.fst()) && this.snd().equals(otherPair.snd()) {
    return true;
} else {
    return false;
}