[英]java interface with multiple implementation classes having different signature
I am creating an interface say '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
}
Now i am creating my implimentation classes say SimpleCar and AdvanceCar where 现在,我正在创建我的实现类,例如SimpleCar和AdvanceCar,其中
Now when i write my implementation classes i am forced to code for both the methods even though i do not want them in my implementation classes 现在,当我编写实现类时,即使我不希望在实现类中使用它们,也不得不为这两种方法编写代码
public class SimpleCar implements Car {
public void drive(int Speed, int Gear ){ ... }; // dont want this method in SimpleCar
public void drive(int Speed ){ ... };
}
can someone help me design my interface which has a method but the implementation classes have different signatures? 有人可以帮助我设计具有方法但实现类具有不同签名的接口吗?
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);
}
}
You should have a Car
interface and another one named GearCar
interface which extends Car
interface. 您应该有一个
Car
接口和另一个名为GearCar
接口的接口,该接口扩展了Car
接口。
This way you can either implement GearCar or Car
interface. 这样,您可以实现
GearCar or Car
接口。
See following design. 请参阅以下设计。 I've removed
gear
from Car
interface cause based on your requirements it's not valid for all cars and hence can't be part of interface. 根据您的要求,我已从“
Car
界面原因中删除了gear
,因为它并非对所有汽车都有效,因此不能成为界面的一部分。
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
}
Write an CarAdapter
providing empty implementations of all methods in the interface. 编写一个
CarAdapter
提供接口中所有方法的空实现。 Then let your SimpleCar
extend CarAdapter
(which by default implements Car
) 然后让您的
SimpleCar
扩展CarAdapter
(默认情况下实现Car
)
This is frequently seen in Swing applications. 这在Swing应用程序中很常见。
Just remove the method declaration that needs Gear
because in your class you need to implement every method declared in the interface that you are implementing unless your class itself is abstract. 只需删除需要
Gear
的方法声明即可,因为在您的类中,您需要实现在实现的接口中声明的每个方法,除非您的类本身是抽象的。
Also you should have your SimpleCar
and AdvancedCar
as abstract class if you don't want to implement any methods of the interface. 如果您不想实现接口的任何方法,则还应该将
SimpleCar
和AdvancedCar
作为抽象类。
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 ){ ... };
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.