问题出在我的讲师界面上吗?

时间:2019-09-15 22:33:58

标签: java interface

我分配了一个实现名为“ CarInterface”的接口的实现。我的班级将被称为“ Car”,并且将在另一个名为“ CarTester”的类中对其进行测试。

但是,界面中的一种方法存在小问题。

此方法称为“等于”,它接受CarInterface参数。在CarTester主程序中,似乎他希望此方法检查品牌,型号,颜色和年份数据是否相同(仅这些属性)。尽管如此,CarInterface还没有任何获取信息的方法!现在我处于停滞状态,正在考虑他的界面是否有问题。

我首先认为可以将CarInterface参数转换为Object,然后将其传递给this.equals(...)。那时我才意识到这里有问题。

(此处是界面代码的摘录) (该接口甚至没有获取VIN的方法)

/**
     * Checks to see if the calling Car and the argument Car have the same state except for the VIN
     * Precondition: Both the calling Car and argument Car are fully initialized
     * Postcondition: no change
     * @param pCarObject
     * @return
     * returns true if the calling Car and the argument Car have the same state except for the VIN, else returns false
     */
    public boolean equals(CarInterface pCarObject);

这是完整的接口代码:

package interfacePackage;
public interface CarInterface 
{
    /**
     * This module changes the color of the car to the color of the argument.
     * Precondition: a state variable that holds the color of the Car
     * Postcondition: The value of the color of the car is now set to the value contained in the parameter.
     * @param pColor
     */
    public void paint(String pColor);

    /**
     * Fills the car fuel tank
     * Precondition: Car has a fuel tank
     * Postcondition: Car's fuel tank is full
     * @return
     * Amount of fuel used to fill tank
     */
    public double fillTank();

    /**
     * Adds fuel to the car's fuel tank
     * Precondition: Car has a fuel tank
     * Postcondition: Car's fuel tank may have added fuel
     * @return
     * Negative number indicating the amount of fuel the tank will still take
     * Positive value of the amount of argument fuel not used, if 0 it just filled the tank
     */
    public double fillTank(double pFuel);

    /**
     * Converts the Car object's state variables to a String representation
     * Precondition: All state variables are initialized
     * Postcondition: no change
     * @return
     * year, make, model, color, VIN
     */
    public String toString();

    /**
     * Checks to see if the calling Car and the argument Car have the same state except for the VIN
     * Precondition: Both the calling Car and argument Car are fully initialized
     * Postcondition: no change
     * @param pCarObject
     * @return
     * returns true if the calling Car and the argument Car have the same state except for the VIN, else returns false
     */
    public boolean equals(CarInterface pCarObject);

    /**
     * drives the Car predefined distance and speed.
     * Precondition: Car's trip state variables have been initialized
     * Postcondition: Car's fuel is reduced proportional to the distance and speed driven or depleted if the distance and speed are too great. odometer and trip odometer are updated with the miles traveled added.
     * @return
     * true if the car travels the distance with fuel remaining, false if the car runs out of fuel
     */
    public boolean driveCar();

    /**
     * gets trip odometer
     * Precondition: none
     * Postcondition: no change of state
     * @return
     * double value of trip odometer to nearest tenth of mile
     */
    public double getTripOdometer();

    /**
     * sets trip odometer mileage to 0.0
     * Precondition: none
     * Postcondition: trip odometer set to 0.0
     */
    public void clearTripOdometer();

    /**
     * gets odometer mileage
     * Precondition: none
     * Postcondition: no change to state
     * @return
     * double value of odometer to nearest tenth of mile
     */
    public double getOdometer();

    /**
     * retrieves fuel level in gallons
     * Precondition: fuel level is initialized
     * Postcondition: no change is state
     * @return
     * fuel level in gallons with decimal values
     */
    public double getFuelLevel();

    /**
     * Car's state is set to hold the speed of travel and distance to travel at that speed
     * Precondition: none
     * Postcondition: Car's state holds information on distance to travel and speed to travel
     * @param pAverageSpeed
     * @param pDrivingDistance
     */
    public void setUpTrip(double pAverageSpeed, double pDrivingDistance);



}

3 个答案:

答案 0 :(得分:1)

据我所知,看来equals(CarInterface pCarObject)方法应该可以从接口解压缩pCarObject中存在的toString()信息,删除VIN,然后与从中获得的信息进行比较。这是一种讨厌的方式,但是我猜

在@racraman所说的话的基础上,这样的事情将从不在专业环境下飞行(至少,不应从不飞行)。 toString()专用于产生人类可读的输出,而不是用于内部传输数据。与使用getDetails()相比,即使是痛苦的toString()方法也可以返回隐秘数组中的所有内容。

处理此问题的理想方法是使用getter和setter,这样您就可以花时间准确地获取所需的东西,没有更多也没有更少。如果吸气剂需要做大量工作才能将值转换为输出,这特别有用,这样您就不必浪费时间转换刚刚被丢弃的输出。

答案 1 :(得分:1)

接口定义了合同,而不是实现。

如果合同要求比较状态(制造商,型号,颜色等),则不会阻止您在Car类中定义这些字段,这是CarInterface合同的实现,因此可以履行合同。

这就是您需要做的;声明字段所隐含状态的字段(制造商,模型等)。您不必定义访问器方法(获取器),但这样做似乎很合理。

实现接口并不仅限于您拥有该接口的方法,而且它也没有说明该类可能具有或不具有的任何字段。

答案 2 :(得分:0)

详细说明波希米亚的答案。

您的Car对象通常只能与另一个Car对象相等。

很公平,如果传入的对象不是汽车,则可以立即拒绝它。这对于默认的Java equals实现非常普遍。 您知道另一个对象是一辆汽车,然后可以将其投射到汽车上,并获取另一辆汽车的年份,品牌,型号和颜色,并进行比较。

我没有提到使用toString进行对象比较。