我读过你不能在泛型类中声明静态变量/方法,我真的不知道如何解决我的问题或解决它,所以我请教你的指导。
我想要的是一个通用的索引,我所有的核心类都会扩展。 我创建了一个游戏引擎,一个例子是,我将拥有不同的游戏状态,这些游戏状态均扩展 State ,然后扩展 Nexus< ;状态> 。我想要静态头部和尾部的原因是,我可以保留所有游戏状态的链接列表,因为它们都是在创建时添加到列表中的。
另一个示例是我将拥有不同的gameobjects,它们都扩展 GameObject ,然后扩展 Nexus< GameObject> 。
这是名为 Nexus 的指数:
公共抽象类Nexus< T> { private static T head = null; private static T tail = null; private T next = null; private static int num = 0; 保护Nexus(){this.Add((T)this); } public T Add(T obj)( ((Nexus)obj).next = null; if(num ++ == 0)head = tail = obj; else tail =(tail.next = obj); return obj; } }另一个解决方案或解决方法我都是耳朵!
解决方案尝试这种方法:定义一个受保护的摘要子类实现的方法,用于为 类类返回一个静态对象。
可能存在一些逻辑问题等,但答案的基础在这里(即编译): <编辑:现在授权给HeadAndTail
/ **< T> Nexus * / 抽象类的一个子类Nexus< T扩展了Nexus< T>> {//这个语法可以让你将T限制在Nexus private T的子类中; 保护Nexus(){ this.add((T)this); $ b $ public T add(T obj){ //委托给HeadAndTail return getHeadAndTail()。add(obj); } / ** @return类的静态* / protected abstract HeadAndTail< T> getHeadAndTail(); } / **为简化API * / 类而绑定到一个对象中HeadAndTail< T扩展了Nexus< T>> { T head = null; T tail = null; int num = 0; public T add(T obj){ obj.next = null; if(num ++ == 0) head = tail = obj; else tail = tail.next = obj; return obj; } } 类ConcreteNexus扩展了Nexus< ConcreteNexus> { //这是静态对象所有实例将从方法返回 private static HeadAndTail< ConcreteNexus> headAndTail = new HeadAndTail< ConcreteNexus>(); 保护HeadAndTail< ConcreteNexus> getHeadAndTail(){返回headAndTail; //返回静态} }
I've read that you cannot declare static variables/methods inside a generic class and I really have no idea how to solve my problem or work around it so I ask for your guidance.
What I want is a generic "index" that all of my core classes will extend. I'm creating a game-engine and an example is that I will have different gamestates who all extends State who in turn extends Nexus<State>. The reason I want the static head and tail is so that I can keep a linked list of all gamestates since they're all added to that list upon creation.
Another example is that I will have different gameobjects who all extends GameObject who in turn extends Nexus<GameObject>.
This is the index called Nexus:
public abstract class Nexus<T> { private static T head = null; private static T tail = null; private T next = null; private static int num = 0; protected Nexus() { this.Add( (T)this ); } public T Add( T obj ) { ((Nexus)obj).next = null; if( num++ == 0 ) head = tail = obj; else tail = ( tail.next = obj ); return obj; } }If anyone got another solution or a workaround I'm all ears!
解决方案Try this approach: Define a protected abstract method that subclasses implement to return a static object for their class.
There may be some logic issues etc, but the basics of the answer are here (ie this compiles):
EDITED: Now delegating to HeadAndTail
/** <T> A subclass of Nexus */ abstract class Nexus<T extends Nexus<T>> { // This syntax lets you confine T to a subclass of Nexus private T next; protected Nexus() { this.add((T) this); } public T add(T obj) { // Delegate to HeadAndTail return getHeadAndTail().add(obj); } /** @return a static for the class */ protected abstract HeadAndTail<T> getHeadAndTail(); } /** Bundled into one Object for simplicity of API */ class HeadAndTail<T extends Nexus<T>> { T head = null; T tail = null; int num = 0; public T add(T obj) { obj.next = null; if (num++ == 0) head = tail = obj; else tail = tail.next = obj; return obj; } } class ConcreteNexus extends Nexus<ConcreteNexus> { // This is the static object all instances will return from the method private static HeadAndTail<ConcreteNexus> headAndTail = new HeadAndTail<ConcreteNexus>(); protected HeadAndTail<ConcreteNexus> getHeadAndTail() { return headAndTail; // return the static } }
更多推荐
在泛型类中声明静态泛型变量
发布评论