有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在存储库模式中解决这个一般问题?

我有一个类型转换器,它可以将字符串转换为对象,反之亦然,类型转换器会考虑本地。为了方便地实现几个类型转换器,例如for BigDecimalPoint,等等,我决定让这个接口通用

public interface TypeConverter<T extends Object>
{ 
    convertToString(Locale locale, T object);
}

这是伟大的实现,因为你可以确保你只得到所需的T,而不必演员或其他东西

convertToString(Locale locale, BigDecimal bigDecimal) { ... }

为了检索正确的转换器,我建立了一个类型转换器存储库,您可以在其中访问特定的类型转换器

 typeConverterRepository.getTypeConverter(sourceValue.getType())

类型转换器存储库为我们提供了正确的类型转换器

现在我们要称之为转换器:

TypeConverter typeConverter = typeConverterRepository.get( ....)
typeConverter.convertToString(context.getLocale(), sourceValue.getValue());

这导致了一个eclipse警告:

The method convertToString(Locale, capture#2-of ?) in the type TypeConverter<capture#2-of ?> is not applicable for the arguments (Locale, Object)

如果不使用@SupressWarning注释,如何解决这个问题?谢谢!


共 (1) 个答案

  1. # 1 楼答案

    我认为问题在于你的typeConverterRepository模式

    执行如下所示的typeconverter

    public class TypeConverterRepository {
        public <T> void registerTypeConverter(Class <T> type, TypeConverter<T> typeConverter);
        public <T> TypeConverter<T> getTypeConverter(Class <T> type);
    }
    

    然后你可以让萨弗里做一个

    TypeConverter<MyClass> typeConverter = typeConverterRepository.getTypeConverter(MyClass.class);