[英]How to implement a Generic method
I need to implement the method as generic. 我需要将该方法实现为泛型。 How to do it ?
怎么做 ?
public SelectItem[] criarFiltroSexo(Sexo[] tp){
SelectItem[] options = new SelectItem[tp.length];
for(int i = 0; i < tp.length; i++) {
options[i] = new SelectItem(tp[i].getId(),tp[i].getDescricao());
}
return options;
}
I have to implement more methods like this and I need to replicate for each different entity.So I have a lot of methods criarFiltroxxx 我必须实现更多像这样的方法,我需要为每个不同的实体复制。所以我有很多方法criarFiltroxxx
I would like to implement something like this : 我想实现这样的事情:
public <E> criarFiltrogenerico(E[] tp){
SelectItem[] options = new SelectItem[tp.length];
for(int i = 0; i < tp.length; i++) {
options[i] = new SelectItem(tp[i].getId(),tp[i].getDescricao());
}
return options;
}
But I got error message : return type of method is missing..... 但我收到错误信息:返回类型的方法丢失.....
But I got error message : return type of method is missing.....
但我收到错误信息:返回类型的方法丢失.....
In the line: 在线:
public <E> criarFiltrogenerico(E[] tp) {
What you have is public
keyword, <E>
type variable and criarFiltrogenerico(E[] tp)
method name and parameters. 你拥有的是
public
关键字, <E>
类型变量和criarFiltrogenerico(E[] tp)
方法名称和参数。
Naturally, you lack the return type. 当然,你缺乏回报类型。 Add it:
添加它:
public <E> RETURN-TYPE-HERE criarFiltrogenerico(E[] tp) {
So this would work: 这样可行:
public <E> SelectItem[] criarFiltrogenerico(E[] tp) {
SelectItem[] options = new SelectItem[tp.length];
for(int i = 0; i < tp.length; i++) {
options[i] = new SelectItem(tp[i].getId(),tp[i].getDescricao());
}
return options;
}
The caveat is that the type E
must have the getId()
and getDescricao()
methods. 需要注意的是,类型
E
必须具有getId()
和getDescricao()
方法。 If you have a parent class/interface with those methods, say ParentItem
(where Sexo extends ParentItem
or Sexo implements ParentItem
), you should use: 如果您有这些方法的父类/接口,比如
ParentItem
(其中Sexo extends ParentItem
或Sexo implements ParentItem
),您应该使用:
public <E extends ParentItem> SelectItem[] criarFiltrogenerico(E[] tp) {
Generally speaking, there are two ways you could approach this problem without having to rewrite the code for every type: 一般来说,有两种方法可以解决这个问题,而无需为每种类型重写代码:
You use Generics. 你使用泛型。 This would mean having the entire object which includes your
criarFiltroSexo(...)
function and creating one object for each class you want to use it with. 这意味着让整个对象包含你的
criarFiltroSexo(...)
函数,并为你想要使用它的每个类创建一个对象。 If criarFiltroSexo
is in a class called DoStuff
and Sexo
extends ParentClass
(as do all the other classes that you want to use that function with) for example, you would implement it like this: 如果
criarFiltroSexo
在一个名为DoStuff
的类中,并且Sexo
扩展了ParentClass
(就像你想要使用该函数的所有其他类一样),你可以像这样实现它:
public class DoStuff<T extends ParentClass> { //... public SelectItem[] criarFiltroSexo(T[] tp){ SelectItem[] options = new SelectItem[tp.length]; for(int i = 0; i < tp.length; i++) { options[i] = new SelectItem(tp[i].getId(),tp[i].getDescricao()); } return options; } //... }
Use inheritance instead. 请改用继承。 Then your function would look like this:
然后你的函数看起来像这样:
public SelectItem[] criarFiltroSexo(ParentClass[] tp){ SelectItem[] options = new SelectItem[tp.length]; for(int i = 0; i < tp.length; i++) { options[i] = new SelectItem(tp[i].getId(),tp[i].getDescricao()); } return options; }
Now you can call that with arrays of everything that extends ParentClass
, eg Sexo[] sexos = new Sexo[10]
. 现在你可以使用扩展
ParentClass
的所有数组来调用它,例如Sexo[] sexos = new Sexo[10]
。
Create an interface that has the methods you need to use: 创建一个包含您需要使用的方法的接口:
interface HasIdAndDescricao {
int getId();
String getDescricao();
}
Add that interface to the classes you want to use with the method: 将该接口添加到要与方法一起使用的类中:
class Sexo implements HasIdAndDescricao {
// no change required
}
Bound the generic to the interface: 将通用绑定到接口:
public <T extends HasId> SelectItem[] criarFiltrogenerico(T[] tp){
SelectItem[] options = new SelectItem[tp.length];
for(int i = 0; i < tp.length; i++) {
options[i] = new SelectItem(tp[i].getId(), tp[i].getDescricao());
}
return options;
}
You may want to use a varargs parameter instead, without changing the method body: 您可能希望使用varargs参数,而无需更改方法体:
public <T extends HasId> SelectItem[] criarFiltrogenerico(T... tp){
// same body
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.