这里有一个简单的例子:Java泛型 - 何时需要参数化?
class World<S extends Sprites, B extends Bodies> {
// Both of these have a problem...
static World world = null; // "World is a raw type. References to generic type
// World<BOD,SPRT> should be parameterized"
static World<S, B> world = null; // "Cannot make a static reference to the non-static type S
// "Cannot make a static reference to the non-static type B
// The following is allowed but looks ugly/fishy to me
static World<?, ?> world = null; //
如果你想知道,我想获得一个单例模式去。挑战在于这个类在一个单独的包GAME中进行了扩展,并且我不能让这个包(MODEL)对该GAME有任何依赖。因此构造函数必须是MODEL。因此,构造函数可以将单例对象存储在MODEL中该类的静态变量中。
static World<?, ?> world = null;
是否确实丑陋在你看来,有没有更好的方法来解决这个问题?谢谢!
请记住,Java有类型擦除;无论有多少种不同的方式来实例化World类(因为它们都与运行时一样共享相同的类),只有一个静态的'world'变量。 – 2012-02-17 20:02:55
我不会让世界成为单身/静态。当你在两个世界存在的情况下写一个不同的游戏时会发生什么?无论如何,在我看来,这取决于你的'GAME'包中的任何内容,以确保_it_只创建一个'World'。这在现在的方式回答你的问题,它避免了这个问题。 – 2012-02-17 20:33:48
好点...其实我的GAME包实现了一个传统的单身人士,以确保只有一个... – pitosalas 2012-02-18 22:44:57