Java 如何创建接受任何类型参数的泛型方法?
我有一个抽象的超级服务,它应该执行一些常见的逻辑。有几个服务实现了这个超级服务。我根据一个条件选择了ServiceImpl,并希望将其分配给抽象类型,以便以后运行公共逻辑 下面的问题是什么?我想将属于/扩展Java 如何创建接受任何类型参数的泛型方法?,java,generics,design-patterns,abstract-class,Java,Generics,Design Patterns,Abstract Class,我有一个抽象的超级服务,它应该执行一些常见的逻辑。有几个服务实现了这个超级服务。我根据一个条件选择了ServiceImpl,并希望将其分配给抽象类型,以便以后运行公共逻辑 下面的问题是什么?我想将属于/扩展BaseResponse的任何对象传递到process()方法中,如我的示例中的FirstResponse //superservice abstract class AbstractService<T extends BaseResponse> { public void
BaseResponse
的任何对象传递到process()
方法中,如我的示例中的FirstResponse
//superservice
abstract class AbstractService<T extends BaseResponse> {
public void process(T t) {
//execute logic that is common to a BaseResponse
}
}
//implementations
class FirstService extends AbstractService<FirstResponse extends BaseResponse> {
}
//usage
AbstractService<? extends BaseResponse> myservice = new FirstService(); //chose by condition
myservice.process(new FirstResponse()); //ERROR
//超级服务
抽象类抽象服务{
公共无效流程(T){
//执行BaseResponse通用的逻辑
}
}
//实现
类FirstService扩展了AbstractService{
}
//用法
抽象服务
如果是这样的话,继承提供的灵活性就足够了,您实际上不需要泛型
public void process(BaseResponse t) {
// ...
}
错误的原因是,Java编译器只知道myservice
是抽象服务
如果是这样的话,继承提供的灵活性就足够了,您实际上不需要泛型
public void process(BaseResponse t) {
// ...
}
错误的原因是,Java编译器只知道myservice
是AbstractService,您可以使用以下泛型:
abstract class AbstractService<T extends BaseResponse> {
public void process(T t) {
//execute logic that is common to a BaseResponse
}
}
//implementations
class FirstService extends AbstractService<FirstResponse> {
@Override
public void process(FirstResponse firstResponse) {
super.process(firstResponse);
...
}
}
public static void main(String[] args) {
//usage
AbstractService<FirstResponse> myservice = new FirstService();
myservice.process(new FirstResponse());
}
抽象类抽象服务{
公共无效流程(T){
//执行BaseResponse通用的逻辑
}
}
//实现
类FirstService扩展了AbstractService{
@凌驾
公共作废流程(FirstResponse FirstResponse){
超级过程(第一反应);
...
}
}
公共静态void main(字符串[]args){
//用法
AbstractService myservice=新的FirstService();
process(newfirstresponse());
}
您可以使用以下泛型:
abstract class AbstractService<T extends BaseResponse> {
public void process(T t) {
//execute logic that is common to a BaseResponse
}
}
//implementations
class FirstService extends AbstractService<FirstResponse> {
@Override
public void process(FirstResponse firstResponse) {
super.process(firstResponse);
...
}
}
public static void main(String[] args) {
//usage
AbstractService<FirstResponse> myservice = new FirstService();
myservice.process(new FirstResponse());
}
抽象类抽象服务{
公共无效流程(T){
//执行BaseResponse通用的逻辑
}
}
//实现
类FirstService扩展了AbstractService{
@凌驾
公共作废流程(FirstResponse FirstResponse){
超级过程(第一反应);
...
}
}
公共静态void main(字符串[]args){
//用法
AbstractService myservice=新的FirstService();
process(newfirstresponse());
}
FirstService
是否与BaseResponse
或BaseResponse
的任何子类或特定子类一起工作?我认为AbstractServiceFirstService
将与BaseResponse
一起工作<代码>?super
将使调用myservice.process()
成为可能,但我无法再分配FirstService
。FirstService
是否与BaseResponse
或BaseResponse
的任何子类一起工作,还是一个特定的子类?我认为AbstractServiceFirstService
将与BaseResponse
一起工作<代码>?super
将使调用myservice.process()
成为可能,但随后我无法再分配FirstService
。。但有了它,我就无法编写:AbstractService myservice=new SecondService()代码>如果第二类服务
。我想根据条件选择服务,然后运行myservice.process
无论服务是什么。未选中的强制转换是我可以想到的一种简单方法AbstractService myservice=(AbstractService)new FirstService()但是,我不鼓励使用代码>,但举个例子,我无法编写:AbstractService myservice=newsecondservice()代码>如果第二类服务
。我想根据条件选择服务,然后运行myservice.process
无论服务是什么。未选中的强制转换是我可以想到的一种简单方法AbstractService myservice=(AbstractService)new FirstService()但不鼓励使用代码>。
FirstService myservice = new FirstService();
myservice.process(new FirstResponse());
abstract class AbstractService<T extends BaseResponse> {
public void process(T t) {
//execute logic that is common to a BaseResponse
}
}
//implementations
class FirstService extends AbstractService<FirstResponse> {
@Override
public void process(FirstResponse firstResponse) {
super.process(firstResponse);
...
}
}
public static void main(String[] args) {
//usage
AbstractService<FirstResponse> myservice = new FirstService();
myservice.process(new FirstResponse());
}