[英]Java Parametrized method explanation
I would like to implement a Generic method inside a CommonInterface
to implement it with different parameters and return Type.我想在CommonInterface
实现一个通用方法,以使用不同的参数实现它并返回类型。
Therefore I have:因此我有:
public interface Common {
public <T, N> T call(N n);
}
If I understand well Generics logic (maybe not..) the call method return a Type T and receive as parameter a Type N. Therefore:如果我理解泛型逻辑(可能不是..),调用方法返回一个类型 T 并接收一个类型 N 作为参数。因此:
public class Class implements Common {
@Autowired private SomeService someService;
@Override
public <ClassDTO, Integer> ClassDTO call(Integer id) {
String s = someService.doSome(id); // suppose doSome receive an Integer as param
}
}
DoSome method is a simple method that receive as input parameter an Integer: DoSome 方法是一个简单的方法,它接收一个整数作为输入参数:
@Service
public class SomeService {
// some @Autowired other service
public String doSome(Integer id) {
// some operation
}
}
I receive an error on doSome(id) call who said me:我在 doSome(id) 调用中收到一个错误,他说我:
The method doSome(java.lang.Integer) in the type SomeService is not applicable for the arguments (Integer)
It's like if:就像如果:
public <ClassDTO, Integer> ClassDTO call(Integer id) {
}
The Integer
above is not recognized as java.lang.Integer
.上面的Integer
不被识别为java.lang.Integer
。 I try to put java.lang.Integer
as follows:我试着把java.lang.Integer
如下:
public <ClassDTO, java.lang.Integer> ClassDTO call(Integer id) {
}
but obtains other errors..但获得其他错误..
What's wrong??怎么了??
If you look closer to the override of call
:如果您更接近call
的覆盖:
public <ClassDTO, Integer> ClassDTO call(Integer id) {
String s = someService.doSome(id); // suppose doSome receive an Integer as param
}
you could see that ClassDTO
and Integer
are not real types, but type parameters (like T
and N
), which are called that way.你可以看到ClassDTO
和Integer
不是真正的类型,而是类型参数(如T
和N
),它们被称为这种方式。 Not following the convention of naming type parameters can lead do the confusion you have.不遵循命名类型参数的约定可能会导致您的困惑。
The override is more clear when it's just like:当它就像这样时,覆盖更清楚:
@Override
public <T, N> T call(N n) {
...
}
The types which replace the type parameters ClassDTO
and Integer
will be the ones, with which you call the call()
method:替换类型参数ClassDTO
和Integer
的类型将是您调用call()
方法的类型:
Class instance = new Class();
Integer integer = 5;
ClassDTO result = instance.<ClassDTO, Integer>call(integer);
This, however, will not resolve the compile-error when calling service.doSome(id)
.但是,这不会在调用service.doSome(id)
时解决编译错误。
In order to make it compliling you can parametrize the Common
interface with T
and N
and then your Class
can implement the generic interface with specific types ( CommonDTO
and Integer
):为了使其编译,您可以使用T
和N
参数化Common
接口,然后您的Class
可以使用特定类型( CommonDTO
和Integer
)实现通用接口:
public interface Common<T, N> {
T call(N n);
}
The Class
will then look like: Class
将如下所示:
public class Class implements Common<CommonDTO, Integer> {
private Service service;
@Override
public CommonDTO call(Integer n) {
return service.doSome(n);
}
}
which now compiles just fine.现在编译得很好。
You're simply missing the fact that Integer
in the body of your call
method override is not the same as java.lang.Integer
.您只是忽略了call
方法覆盖主体中的Integer
与java.lang.Integer
的事实。
This happens often with generics when the simple single-uppercase-letter type variable naming convention is skipped.当跳过简单的单大写字母类型变量命名约定时,泛型经常发生这种情况。
The parameter Integer id
is not of the same type as the one taken by doSome
, presumably java.lang.Integer
.参数Integer id
与doSome
所采用的doSome
,大概是java.lang.Integer
。
Side note: you're hiding implicitly imported classes ( Class
, Integer
), which is known to lead to bugs;旁注:您隐藏了隐式导入的类( Class
, Integer
),众所周知,这会导致错误; and that's beside not following the naming convention for type variables.除了不遵循类型变量的命名约定之外。 Conventions sometimes help prevent bugs!约定有时有助于防止错误!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.