[英]How do I correctly implement an interface with a method that has Object as an argument?
I'm writing a program our professor has given us as homework. 我正在写一个教授给我们做的作业的程序。
I have a class called Car
, that has two private real members x
and y
, which are supposed to represent the coordinates of the car in the coordinate plane, and I wanted to implement an interface called MyInterface
, in which I declared a method public boolean compare(Object t)
. 我有一个名为Car
的类,它具有两个私有实成员x
和y
,它们分别表示汽车在坐标平面中的坐标,并且我想实现一个名为MyInterface
的接口,在其中我声明了一个方法public boolean compare(Object t)
It was stated it has to be Object t
and not something else, so that I can implement the interface in multiple classes (part of the homework). 据说它必须是Object t
而不是其他Object t
,以便我可以在多个类(作业的一部分)中实现接口。 Java is the programming language in question. Java是有问题的编程语言。
The part of the code that that is problematic looks like this: 有问题的代码部分如下所示:
public interface MyInterface {
public boolean compare(Object t);
}
public class Car implements MyInterface{
private double x, y;
@Override
public boolean compare(Car t) {
double curdist = Math.sqrt(this.x*this.x + this.y*this.y);
double givdist = Math.sqrt(t.x*t.x + t.y*t.y);
if(curdist < givdist) return true;
else return false;
}
}
My method compare
needs to calculate the distances from the point (0,0) for the current car and the given car, and compare the two distances. 我的方法compare
需要计算当前汽车和给定汽车到点(0,0)的距离,并比较两个距离。 If the current car's distance ( curdist
) is closer from the given car's distance ( givdist
) to (0,0), compare
returns true, otherwise it returns false. 如果当前汽车的距离( curdist
)与给定汽车的距离( givdist
)接近(0,0),则compare
返回true,否则返回false。
My question is, how do I correctly implement this method in the class Car? 我的问题是,如何在Car类中正确实现此方法? I've tried a few things, but none of them seem to work out. 我已经尝试了一些方法,但是似乎都没有解决。 Any help is appreciated, 'cause I am quite new to object-oriented programming. 感谢您提供任何帮助,因为我对面向对象的编程非常陌生。 Thanks in advance. 提前致谢。
If you must have Object
as the parameter type for compare
, then you must test the class of the parameter passed in to make sure it's a Car
first. 如果必须具有Object
作为compare
的参数类型,则必须测试传入的参数的类以确保它首先是Car
。
Change the compare
argument to Object
in your Car
class. 在您的Car
类中将compare
参数更改为Object
。 Then the @Override
annotation will determine that the method is properly overriding the instance method, and you won't get a compiler error. 然后@Override
批注将确定该方法正确覆盖了实例方法,并且不会出现编译器错误。 Use the instanceof
operator to determine if t
is a Car
. 使用instanceof
运算符确定t
是否为Car
。 You may decide to throw an IllegalArgumentException
if it isn't a Car
, or if it's null
. 如果它不是Car
,或者它为null
,则可以决定引发IllegalArgumentException
。 Then you can cast it to a Car
and continue with the logic of your method. 然后,您可以将其转换为Car
并继续执行方法的逻辑。
Even if you have to do have it take Object
for your class, a better way to do this in general is with generics. 即使必须将Object
用作类,一般而言,更好的方法是使用泛型。 Here, this would answer the question "What is this class comparable to ?". 在这里,这将回答这个问题:“这是什么类媲美 ?”。 If you've covered generics, then you may understand a class that is defined as MyInterface<T>
. 如果您已经介绍了泛型,则可以理解定义为MyInterface<T>
。 The compare
method would take a parameter of type T
. compare
方法将采用类型T
的参数。 This allows you to implement the method in Car
with a parameter of type Car
, provided Car implements MyInterface<Car>
. 这样就可以实现在方法Car
用类型的参数Car
,提供Car implements MyInterface<Car>
。 This is similar to the existing Comparable
interface built-in to Java. 这类似于Java内置的现有Comparable
接口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.