简体   繁体   中英

Passing Type Parameters using Generics in Java

I am trying to make some more sense of Generics, and I have the following situation:

// This is one of many Models I have that want to use like this, or similar
public class MyData{
    private String name;
    private String color;
    //... default constructor    

    //... setters and getters

}

and I have an abstract class:

public abstract class CacheAble<T>{

   // The idea is any T can be inserted
   public abstract <T> boolean insertCacheAble(T data);

   // The idea is any T can be deleted
   public abstract <T> boolean deleteCacheAble(T data);      

}

So what I would like to do is the following:

 // Have my Operations for how i want to handle the methods on MyData
 public MyDataOps extends CacheAble<MyData>{

   //... Members

   public MyDataOps(){
       //.. initialize some other members
   }      

   @Override
   public <T> boolean insertCacheAble(T data){
       //... want to be able to Access MyData.name & MyData.color
       //... how can I do this?

      String name = data.getName();
      String color = data.getColor();

      //.. insert db values
   }

   @Override
   public <T> boolean deleteCacheAble(T data){

      String name = data.getName();
      String color = data.getColor();

      //... Perform db delete based on name or color
   }

 }

I am not very good with Java Generics as you can see so any pointing in the right direction would be very helpful! Should I implement this instead as a generic interface?

In your code

public abstract class CacheAble<T>{
   public abstract <T> boolean insertCacheAble(T data);
   public abstract <T> boolean deleteCacheAble(T data);      
}

the type parameter T of the methods is not the same T as the class type parameter. That's probably not what you want. Firstly, it makes the methods more generic than you want (you want their signature to be fixed, if you have fixed the class' type parameter). Secondly, that forces you to use a type parameter in the implementation of those methods in subclasses, even though they should work on a specific type. So define it like this:

public abstract class CacheAble<T>{
   public abstract boolean insertCacheAble(T data);
   public abstract boolean deleteCacheAble(T data);
}

Also, as "sodik" has pointed out, your subclass is specific to a certain type of T and therefore should no longer make any reference to T , but only to the specific type MyData . Then you can use the public methods of MyData (like eg getters and setters for color and name):

public MyDataOps extends CacheAble<MyData>{
   @Override
   public boolean insertCacheAble(MyData data){
       // ... data.getName(); ...
       // ... data.getColor(); ...
   }

   @Override
   public boolean deleteCacheAble(MyData data){
       // ... data.getName(); ...
       // ... data.getColor(); ...
   }
}
String name = data.getName();
String color = data.getColor();

You can't access the fields directly since they are private.

@Override
   public <MyData> boolean insertCacheAble(MyData data){
       //... want to be able to Access MyData.name & MyData.color
       //... how can I do this?

      String name = data.name; // This doesn't work
      String color = data.color; // This doesn't work

      //.. insert db values
   }

T is not a class you have implemented with the private members of name or color. MyData is valid however. Replace your T object parameter with a MyData object and everything should work just fine.

public class MyData{
    public Hashmap<String,Object> dataField;
    //... default constructor    

    //... setters and getters

}

...

@Override
   public boolean insertCacheAble(MyData data){


      String name = data.dataField.get("name"); 
      String color = data.dataField.get("color");

      //.. insert db values
   }

   let anObject be a <T> with field MyData myData
   insertCacheAble(anObject.myData);

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