2017-07-26 22 views
3

我點名BaseCode和2具體類的抽象類名爲LocationDepartment泛型:如何獲取地圖的值,而鑄造

public abstract class BaseCode { 
    private Integer id; 
    ... 
    public Integer getId() { return id; } 
    public void setId(Integer id) { this.id = id; } 
    ... 
} 

public class Location extends BaseCode { 
    ... 
} 

public class Department extends BaseCode { 
    ... 
} 

我有一個名爲BaseCodeCache和2具體類的抽象類名爲LocationCacheDepartmentCacheLocationCacheDepartmentCache將使用Singleton

public abstract class BaseCodeCache { 
    ... 
} 

public class LocationCache extends BaseCodeCache { 
    ... 
} 

public class DepartmentCache extends BaseCodeCache { 
    ... 
} 

  1. BaseCodeCache,我想有一個java.util.Map,其值可以 是任何類型的BaseCodeLocation對象或Department 對象。
    LocationCache中,我想要java.util.Map存儲Location對象。
    DepartmentCache中,我想要java.util.Map存儲Department對象。

爲了實現這一點,我把這個代碼在BaseCodeCache

private Map<Integer, BaseCode> idMap = new HashMap<Integer, BaseCode>(); 

  • 我希望有存儲在所述值的方法 java.util.Map
  • 要做到這一點,我把這個代碼在BaseCodeCache

    public void add(BaseCode baseCode) { 
        if (baseCode != null) { 
         idMap.put(baseCode.getId(), baseCode); 
        } 
    } 
    

    這是我會怎麼使用它Location

    Location location = new Location(); ... 
    LocationCache.getInstance().add(location); 
    

    這是我會怎麼用它適用於Department

    Department department = new Department(); ... 
    DepartmentCache.getInstance().add(department); 
    

  • 我想有一個方法來獲得在java.util.Map作爲 一個java.util.List所有值。
    LocationCache此方法應返回List<Location>
    DepartmentCache此方法應返回List<Department>
  • 這就是我卡住了。我想在BaseCodeCache中創建此方法,但當通過LocationCache調用此方法時,它會返回List<Location>,並且當通過DepartmentCache調用此相同的方法時,則返回List<Department>這可能嗎?

    我把這個代碼在BaseCodeCache

    public List<BaseCode> getList() { 
        return new ArrayList<BaseCode>(idMap.values()); 
    } 
    

    但上面的代碼返回List<BaseCode>。當我把它叫做這樣:

    List<Location> allLocations = LocationCache.getInstance().getList(); 
    

    那麼Java不會讓它編譯並給予此錯誤消息:

    類型不匹配:不能從List<BaseCode>轉換爲List<Location>

    我可以通過獲取List<BaseCode>然後通過循環將其轉換爲List<Location>來修復,但那看起來不正確。

    可以這樣做嗎?

    public abstract class BaseCodeCache<T extends BaseCode> { 
        private Map<Integer, T> idMap = new HashMap<>(); 
    
        public void add(T baseCode) { 
         if (baseCode != null) { 
          idMap.put(baseCode.getId(), baseCode); 
         } 
        } 
    
        public List<T> getList() { 
         return new ArrayList<>(idMap.values()); 
        } 
    } 
    
    public class LocationCache extends BaseCodeCache<Location> {} 
    
    public class DepartmentCache extends BaseCodeCache<Department> {} 
    

    這一操作將讓您做到以下幾點沒有任何編譯錯誤:

    回答

    4

    使用泛型如下實施

    LocationCache locationCache = new LocationCache(); 
    locationCache.add(new Location()); 
    
    List<Location> locations = locationCache.getList(); 
    

    更妙的是,你如果您嘗試添加或檢索錯誤類型的對象,則會出現編譯錯誤:

    locationCache.add(new Department()); // won't compile 
    List<Department> departments = locationCache.getList(); // won't compile 
    
    +0

    謝謝你。這太棒了。一個問題。是否有您使用的仿製藥的名稱?如果是,請提及。 TIA – srh

    +0

    不知道「泛型類型」是什麼意思。上面的例子實際上只是泛型101,你可以在教程中找到第一個例子。 –

    +0

    謝謝。我發現你使用**有界的類型參數** T. – srh