1

我正在创建一个界面说“汽车”

public interface Car {
    public void drive(int Speed, int Gear ); // for cars which have gears
    public void drive(int Speed); // for cars which do not have gears
}

现在我正在创建我的实施类说 SimpleCar 和 AdvanceCar 在哪里

  • SimpleCar 没有齿轮
  • AdvanceCar 有齿轮

现在,当我编写实现类时,我被迫为这两种方法编写代码,即使我不希望它们出现在我的实现类中

public class SimpleCar implements Car {
    public void drive(int Speed, int Gear ){ ... }; // dont want this method in SimpleCar
    public void drive(int Speed ){ ... };
}

有人可以帮我设计一个有方法但实现类有不同签名的接口吗?

4

6 回答 6

3
public interface Car {

    public void drive(int Speed, int Gear); // for cars which have gears

    public void drive(int Speed); // for cars which do not have gears
}



public class CarAdapter implements Car {

    @Override
    public void drive(int Speed, int Gear) {
        // TODO Auto-generated method stub

    }

    @Override
    public void drive(int Speed) {
        // TODO Auto-generated method stub

    }

}


public class AdvancedCar extends CarAdapter {

    @Override
    public void drive(int Speed) {
        // TODO Auto-generated method stub
        super.drive(Speed);
    }

    @Override
    public void drive(int Speed, int Gear) {
        // TODO Auto-generated method stub
        super.drive(Speed, Gear);
    }

}


public class SimpleCar extends CarAdapter {

    @Override
    public void drive(int Speed) {
        // TODO Auto-generated method stub
        super.drive(Speed);
    }


}
于 2012-10-22T07:22:44.787 回答
2

请参阅以下设计。根据您的要求,我已从gear界面Car原因中删除它不适用于所有汽车,因此不能成为界面的一部分。

public interface Car 
{     
    // public void drive(int Speed, int Gear ); // for cars which have gears     
    public void drive(int Speed); // for cars which do not have gears 
}

public abstract class SimpleCar implements Car
{
    public void drive(int speed) { ... }
    public abstract void accelerate(); // you can move it to interface also
}

public abstract class AdvancedCar implements Car
{
    protected int CURRENT_GEAR = 1;
    public void drive(int speed) { ... }
    public void changeGear(int gear) { ... }
    public abstract void accelerate();
}

public class Reva extends SimpleCar
{
    // provide implementation for accelerate
}

public class Ferrari extends AdvancedCar
{
    // provide implementation for accelerate
}
于 2012-10-22T07:20:48.253 回答
2

您应该有一个Car接口和另一个GearCar扩展接口的命名Car接口。

这样您就可以实现GearCar or Car接口。

于 2012-10-22T07:18:10.530 回答
1

编写一个CarAdapter提供接口中所有方法的空实现。然后让你的SimpleCar扩展CarAdapter(默认实现Car

这在 Swing 应用程序中很常见。

于 2012-10-22T07:15:49.953 回答
0
public interface Vehicle {
    public void drive(int Speed);
}

public interface Car extends Vehicle{
    public void drive(int Speed, int Gear ); 
}

public class SimpleCar implements Vehicle {
    public void drive(int Speed ){ ... };
}

public class AdvancedCar implements Car {
    public void drive(int Speed, int Gear ){ ... };
    public void drive(int Speed ){ ... };
}
于 2012-10-22T07:20:17.363 回答
0

只需删除需要的方法声明,Gear因为在您的类中,您需要实现在您正在实现的接口中声明的每个方法,除非您的类本身是抽象的。

如果你不想实现接口的任何方法,你也应该有你的SimpleCar和作为抽象类。AdvancedCar

于 2012-10-22T07:13:43.600 回答