简体   繁体   中英

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

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.

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. If you have a parent class/interface with those methods, say ParentItem (where Sexo extends ParentItem or Sexo implements ParentItem ), you should use:

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:

  1. 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. 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:

     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; } //... } 
  2. 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] .

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:

public <T extends HasId> SelectItem[] criarFiltrogenerico(T... tp){
    // same body
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM