- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这个问题类似于Factory and generics ,并且可能有相同的答案,但它是不同的。我有一个通用基类,它将由完全独立的 JAR 中的类进行扩展。所述 JAR 应该能够在不更改任何其他代码的情况下放入和替换(代码根据命令执行)。
基类:
public abstract class ThingBase<A> {
public abstract A doThing();
public abstract Thing getThingType();
// This might be the wrong way to do this, and I elaborate below.
public final SortedMap<Long, A> getCollectionContainer() {
return new TreeMap<Long, A>();
}
}
我有一个枚举,我将为每个条目创建不同的事物扩展。
public enum Thing { T1, T2, T3 }
创建此类对象的工厂:
public class ThingFactory {
private static final Map<Thing, Class<? extends ThingInterface<?>>> thingMap;
static{//populate map by finding all extensions of base class}
public static ThingBase<?> getThing(Thing thing) {
try {
Class<? extends ThingBase<?>> thingImpl = thingMap.get(thing);
if (thingImpl != null) {
return thingImpl.newInstance();
} else {
return null;
}
} catch (InstantiationException e) {
return null;
} catch (IllegalAccessException e) {
return null;
}
}
}
我的问题是下面代码中未经检查的原始类型警告(注意不是上面,上面的代码编译得很好)。在以下用法中,代码成功执行,但我收到未经检查的类型警告。
public class ThingAccumulator {
// Raw Type
private final ThingBase myThing;
// Raw Type
private SortedMap myMap;
public ThingAccumulator(Thing thing) {
myThing = ThingFactory.getThing(thing);
myMap = myThing.getCollectionData();
//Type safety: The method put(Object, Object) belongs to the raw type Map. References to generic type Map<K,V> should be parameterized
myMap.put(System.currentTimeMillis(), myThing.doThing());
}
}
放置 < ? > ThingBase 和 SortedMap 字段上的 < Long, ?> 提示 ? 不同:
Map 类型中的 put(Long, capture#17-of ?) 方法不适用于参数 (long, capture#18-of ?)
现在这里的主要问题似乎是编译器无法假设?和 ?是相同的,这是公平的,但到目前为止我想出的唯一解决方案是将类型放入持有类中,这不是一个选项,因为只有实现知道数据类型:/
有人知道如何消除这些警告吗?有更好的方法吗?
谢谢
SDY
最佳答案
您可以将泛型类型参数添加到 ThingAccumulator
,使用它来移动事物,以便 Java 提示的唯一代码是您可以证明确实是类型安全的简单行,然后抑制该警告,因为您知道这不是问题。
为 ThingAccumulator
提供类型参数让它的字段在一起使用时进行类型检查。你可以提出“但是类型是什么?”的问题。在构造函数之外,采用 ThingBase<A>
实例作为参数而不是 Thing
枚举值。然后,静态工厂方法可以处理对象构造:
public class ThingAccumulator<A> {
private final ThingBase<A> myThing;
private SortedMap<Long, A> myMap;
public static ThingAccumulator<?> create(Thing thing) {
return new ThingAccumulator(ThingFactory.getThing(thing));
}
protected ThingAccumulator(ThingBase<A> thing) {
myThing = thing;
myMap = myThing.getCollectionData();
myMap.put(System.currentTimeMillis(), myThing.doThing());
}
}
我希望 Java 喜欢 create()
功能。逻辑可能是这样的:
ThingFactory.getThing()
返回 ThingBase<?>
。这就像 ThingBase<X>
对于一些未知的X
。我们不知道它并不意味着它不存在。new ThingAccumulator()
可以拍ThingBase<X>
,对于任何 X
,并返回 ThingAccumulator<X>
.ThingAccumulator<?>
— 具有未知类型参数的实例。类型安全尚未被破坏。但是,Java 不喜欢它(至少在我的 Java 1.7.0 版本中)。但如果我们喜欢它,我们可以抑制警告:
@SuppressWarnings("unchecked")
public static ThingAccumulator<?> create(Thing thing) {
return new ThingAccumulator(ThingFactory.getThing(thing));
}
并且因为所有其余代码(到目前为止......)都是类型安全的,所以不再有警告。
关于泛型类的 Java 工厂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24318024/
可以用这种方式转换字符串吗?我们有相同的参数,Java 做出了正确的选择。如果值是整数 - 我们调用 parseInt(value),否则如果值是 double 型 - 我们调用 parseDoubl
如果这段代码中有一个愚蠢的错误,我提前道歉,但我似乎无法解决它。我的问题是这样的,我用GCC-8(通过home-brew安装在Mac上)编译,然后在终端中执行。当使用 int do 定义变量 s &
我用 a-videosphere 制作了一个 a-scene。我尝试使用按钮启用/禁用声音,但有些想法不起作用?这是我的代码: var gargamel = 0; function
我正在使用 ISAAC 实现来生成随机整数。我需要用这些整数创建一个高斯值。首先,我需要将它们从 0 更改为 1 的 double 值。我怎样才能在Java中做到这一点?这是到目前为止我将整数转换为
我将 0x0000 到 0x01c2 范围内的十六进制值从 BLE 获取到我的手机 a 作为字符串。为了将其绘制在图表中,我必须将其转换为 double,我已经尝试过 this method但遗憾的是
我有一个父类(super class) Animal和一个子类 Dog 。在第三节课中,我有一个 List它同时接受子类型和父类(super class)型对象。 public class foo{
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 6 年前。 Improve this ques
我正在尝试查询我的用户的距离。我可以用这段代码做到这一点 PFGeoPoint.geoPointForCurrentLocationInBackground { (geoPoint: PFGe
考虑示例:http://jsfiddle.net/KWLu7/16/ 我正在尝试将总体重要性值计算为每个选定选择重要性的总和乘以其父标准重要性: var watch = $scope.$watch("
这个问题在这里已经有了答案: Bounding generics with 'super' keyword (6 个答案) 关闭 2 年前。 我有一个列表装饰器,它应该允许从一个列表转换到另一个列表
为什么下面的代码没有选择最近父类(super class)型的隐式 val? class A class B extends A trait TC[-T] { def show(t: T): Stri
这是我想要做的 def merge[A, B, C](eithers: Either[A,B]*)(implicit ev1: A x, x => x)) 关于scala - 推断常见的父类(s
我正在尝试从具有 double 类型列的Cassandra表中获取 double 值。我已经使用CQL3语法创建了表: CREATE TABLE data_double ( datetime
是否应该在不需要显式类型定义的情况下编译以下 this ? def prepList[B >: A](prefix: PlayList[B]) : PlayList[B] = prefix.fol
我正在查看某人的代码,并且在创建结构时使用了 abstract type AbstractFoo end julia> struct Foo1 struct Foo2 foo_op(x::Abst
一些示例代码: public class Main { class SomeType { } class A { protected T createSome
是否可以只接受类的泛型类型的父类(super class)型? 我正在寻找的是这样的: class MyClass { public void myMethod(TS someObject
在我的代码中,我有许多 ArrayList 被传递到排序方法中。每个 ArrayList 都有不同的泛型类型,但所有这些类型都是 Sorter 的实现。排序方法旨在接受 Sorter 类型的 Arra
如果已经有人问过这个问题,请链接并关闭这个问题。 我目前正在为另一个使用起来复杂得多(并且有潜在危险)的 API 的简化 API 设计原型(prototype)。 考虑到相关的有点复杂的对象创建,我决
我正在尝试构建一个具有某些依赖项的 android 应用程序,但是其中一个导致了此错误: Illegal class file: Class module-info is missing a supe
我是一名优秀的程序员,十分优秀!