java java接口具有多个具有不同签名的实现类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13006521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java interface with multiple implementation classes having different signature
提问by Mrinmoy
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,其中
- SimpleCar do not have gears
- AdvanceCar have gears
- 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?
有人可以帮助我设计具有方法但实现类具有不同签名的接口吗?
回答by Festus Tamakloe
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);
}
}
回答by Azodious
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.
请参阅以下设计。我已根据您的要求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
}
回答by Metalhead
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
接口。
回答by Thorbj?rn Ravn Andersen
Write an CarAdapter
providing empty implementations of all methods in the interface. Then let your SimpleCar
extend CarAdapter
(which by default implements Car
)
编写一个CarAdapter
提供接口中所有方法的空实现。然后让您的SimpleCar
扩展CarAdapter
(默认情况下实现Car
)
This is frequently seen in Swing applications.
这在 Swing 应用程序中很常见。
回答by Abubakkar
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
andAdvancedCar
作为抽象类。
回答by Kojotak
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 ){ ... };
}