第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何處理通用接口實(shí)現(xiàn)并避免@SuppressWarnings?

如何處理通用接口實(shí)現(xiàn)并避免@SuppressWarnings?

慕妹3146593 2023-03-23 16:27:42
我正在嘗試對(duì)不同的服務(wù)實(shí)現(xiàn)進(jìn)行通用處理,但我經(jīng)常收到“由于原始類型而導(dǎo)致未檢查的調(diào)用”錯(cuò)誤。我嘗試了幾種實(shí)現(xiàn),但不太明白這里的問題是什么。我有以下模型:public abstract class Fruit {  public List<String> vitamins;  public String originCountry;  // getters and setters omitted}具體實(shí)現(xiàn)如下:public class Kiwi extends Fruit {}public class Pineapple extends Fruit {}我有以下界面:public interface FruitCheckService<T extends Fruit> {  List<String> compareVitaminsFromDifferentCountries(T firstFruit, T secondFruit);  Class<T> getImplementation();}對(duì)于上面給出的 2 個(gè)模型的實(shí)現(xiàn):@Servicepublic class KiwiCheckServiceImpl implements FruitCheckService<Kiwi> {  @Override  public List<String> compareVitaminsFromDifferentCountries(Kiwi firstFruit, Kiwi secondFruit) {    // some implementation    return new ArrayList<>();  }  @Override  public Class<Kiwi> getImplementation() {    return Kiwi.class;  }}還有菠蘿:@Servicepublic class PineappleCheckServiceImpl implements FruitCheckService<Pineapple> {  @Override  public List<String> compareVitaminsFromDifferentCountries(Pineapple firstFruit, Pineapple secondFruit) {    // some implementation    return new ArrayList<>();  }  @Override  public Class<Pineapple> getImplementation() {    return Pineapple.class;  }}我有以下類正在使用不同的 bean 進(jìn)行操作:@Servicepublic class FruitServices {  private Map<Class, FruitCheckService> beansMap;  @Autowired  public FruitServices(List<FruitCheckService> fruitCheckServices) {    beansMap = new HashMap<>();    fruitCheckServices      .forEach(        fruitCheckService -> {          Class implementation = fruitCheckService.getImplementation();          beansMap.put(implementation, fruitCheckService);        }      );  }  public FruitCheckService getFruitCheckService(Class clazz) {    return beansMap.get(clazz);  }}
查看完整描述

1 回答

?
POPMUISE

TA貢獻(xiàn)1765條經(jīng)驗(yàn) 獲得超5個(gè)贊

問題是您Class在服務(wù)定義中使用原始類型。要解決此問題,您可以使用有界通配符類型 ?,如下所示:


class FruitServices {


  private Map<Class<? extends Fruit>, FruitCheckService<? extends Fruit>> beansMap;



  public FruitServices(List<FruitCheckService<? extends Fruit>> fruitCheckServices) {


    beansMap = new HashMap<>();


    fruitCheckServices

      .forEach(

        fruitCheckService -> {

          Class<? extends Fruit> implementation = fruitCheckService.getImplementation();

          beansMap.put(implementation, fruitCheckService);

        }

      );

  }


  public FruitCheckService<? extends Fruit> getFruitCheckService(Class<? extends Fruit> clazz) {

    return beansMap.get(clazz);

  }

}

對(duì)于課堂來說,BusinessService事情會(huì)變得更加棘手。首先,有必要制定compareVitamins一個(gè)通用方法來確保兩個(gè)水果屬于同一類型。但即使那樣,我也不相信你可以避免 unchecked cast from the result,getFruitCheckService因?yàn)椋捎贔ruitServices必須處理不同類型水果的服務(wù),你不能為 的返回值提供精確的類型參數(shù)getFruitCheckService。


class BusinessService {


    private FruitServices fruitServices;


    public <T extends Fruit> void compareVitamins(T one, T two) {


        @SuppressWarnings("unchecked")

        FruitCheckService<T> fruitCheckService = 

            (FruitCheckService<T>) fruitServices.getFruitCheckService(one.getClass());


        List<String> result = fruitCheckService.compareVitaminsFromDifferentCountries(one, two);

    }

}


查看完整回答
反對(duì) 回復(fù) 2023-03-23
  • 1 回答
  • 0 關(guān)注
  • 106 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)