And*_*rey 6 java generics inheritance
我的问题不容易使用单词来解释,幸运的是,它并不太难以证明.所以,忍受我:
public interface Command<R>
{
public R execute();//parameter R is the type of object that will be returned as the result of the execution of this command
}
public abstract class BasicCommand<R> implements Command<R>
{
}
public interface CommandProcessor<C extends Command<?>>
{
public <R> R process(C<R> command);//this is my question... it's illegal to do, but you understand the idea behind it, right?
}
//constrain BasicCommandProcessor to commands that subclass BasicCommand
public class BasicCommandProcessor<C extends BasicCommand<?>> implements CommandProcessor<C>
{
//here, only subclasses of BasicCommand should be allowed as arguments but these
//BasicCommand object should be parameterized by R, like so: BasicCommand<R>
//so the method signature should really be
// public <R> R process(BasicCommand<R> command)
//which would break the inheritance if the interface's method signature was instead:
// public <R> R process(Command<R> command);
//I really hope this fully illustrates my conundrum
public <R> R process(C<R> command)
{
return command.execute();
}
}
public class CommandContext
{
public static void main(String... args)
{
BasicCommandProcessor<BasicCommand<?>> bcp = new BasicCommandProcessor<BasicCommand<?>>();
String textResult = bcp.execute(new BasicCommand<String>()
{
public String execute()
{
return "result";
}
});
Long numericResult = bcp.execute(new BasicCommand<Long>()
{
public Long execute()
{
return 123L;
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
基本上,我希望通用的"进程"方法指定Command对象的泛型参数的类型.目标是能够将CommandProcessor的不同实现限制为实现Command接口的某些类,同时能够调用实现CommandProcessor接口的任何类的进程方法,并让它返回由parametarized命令对象.我不确定我的解释是否足够清楚,如果需要进一步说明,请告诉我.我想,问题是"这根本可以吗?" 如果答案是"否",最好的解决办法是什么(我自己想到了一对夫妇,但我想要一些新鲜的想法)
不幸的是,你不能这样做。由于您希望CommandProcessor根据 定义接口Command,因此您的实现必须准备好采用任何类型的Command实例 - 泛型不能将其限制为BasicCommand- 如果可以,那么BasicCommandProcessor子类将不会实现该CommandProcessor接口。
或者,从另一个角度来看,给定一个CommandProcessor接口,泛型不可能确保仅通过BasicCommand实例调用它。要做到这一点需要了解实现,并且违背了多态性和接口的观点。
您可以参数化命令的结果,但不能参数化命令的具体类。
public interface Command<R>
{
public R execute();//parameter R is the type of object that will be returned as the result of the execution of this command
}
public abstract class BasicCommand<R> implements Command<R>
{
}
public interface CommandProcessor
{
public <R> R process(Command<R> command);
}
public class BasicCommandProcessor implements CommandProcessor
{
public <R> R processBasicCommand(BasicCommand<R> command)
{
return command.execute();
}
public <R> R process(Command<R> command)
{
return processBasicCommand((BasicCommand<R>)command);
}
}
Run Code Online (Sandbox Code Playgroud)
最简单的方法是提供一个接受您需要的特定类型的方法,并在泛型方法中调用该方法。(请参阅上面的 BasicCommandProcessor。)