gpt4 book ai didi

java - 如何处理通用接口(interface)实现并避免@SuppressWarnings?

转载 作者:行者123 更新时间:2023-11-30 05:33:53 26 4
gpt4 key购买 nike

我试图对不同的服务实现进行通用处理,但我不断收到“由于原始类型而未经检查的调用”错误。

我尝试了几种实现,但不太明白这里的问题是什么。

我有以下模型:

public abstract class Fruit {

public List<String> vitamins;
public String originCountry;

// getters and setters omitted
}

具体实现如下:

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();
}

上面给出的 2 个模型的实现:

@Service
public 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;
}
}

还有菠萝:

@Service
public 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 进行操作:

@Service
public 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);
}
}

最后,这是我调用的服务:

@Component
public class BusinessService {

@Autowired
private FruitServices fruitServices;

public void compareVitamins(Fruit one, Fruit two) {

Class<? extends Fruit> aClass = one.getClass();

FruitCheckService fruitCheckService = fruitServices.getFruitCheckService(aClass);

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

如何正确使用fruitCheckService而不收到“作为原始类型'exercise2.service.FruitCheckService'的成员对'compareVitaminsFromDifferentCountries(T, T)'的未经检查的调用”?

最佳答案

问题是您在服务定义中使用原始类型Class。要解决此问题,您可以使用 bounded wildcard type ?,例如:

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);
}
}

对于 BusinessService 类,事情会变得更加棘手。首先,有必要使 compareVitamins 成为一个通用方法,以确保两种水果属于同一类型。但即便如此,我也不相信您可以避免 getFruitCheckService 结果中的未经检查的强制转换,因为,由于 FruitServices 必须处理不同类型水果的服务,您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);
}
}

关于java - 如何处理通用接口(interface)实现并避免@SuppressWarnings?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57006934/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com