用Java创建一个类数组

时间:2015-01-01 09:25:16

标签: java arrays class static

是否可以在Java中创建静态类数组? 例如:

SceneObject[] scenes = {Loading.class, Menu.class};
// Loading and Menu extend SceneObject

我们需要通过数组调用静态方法,而不是实例化它们。

编辑:

以下是我们正在努力实现的目标。我们也可以使用很多开关,但在每种方法中将每个对象添加到每个开关听起来都是多余的。

package net.bitworm.gameengine;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;

import net.bitworm.scenes.*;

public class SceneController {

public enum Scene{
    LOADING_SCENE,
    MENU,
    SCENE_1
}

public static SceneObject[] scenes = {new Loading(), new Menu()};

public volatile static Scene currentScene = Scene.LOADING_SCENE;

public static void setScene(Scene newScene){
    currentScene = newScene;
    System.out.println("Switched to " + currentScene.toString());
}

public static void update(GameContainer container, int delta){
    scenes[currentScene.ordinal()].update(container, delta);
}
public static void render(GameContainer container, Graphics g){
    scenes[currentScene.ordinal()].render(container, g);
}
public static void mouseMoved(int oldx, int oldy, int newx, int newy){
    scenes[currentScene.ordinal()].mouseMoved(oldx, oldy, newx, newy);
}
public static void mousePressed(int button, int x, int y){
    scenes[currentScene.ordinal()].mousePressed(button, x, y);;
}
public static void mouseReleased(int button, int x, int y){
    scenes[currentScene.ordinal()].mouseReleased(button, x, y);
}
public static void mouseWheelMoved(int change){
    scenes[currentScene.ordinal()].mouseWheelMoved(change);
}
public static void keyPressed(int key, char c){
    scenes[currentScene.ordinal()].keyPressed(key, c);
}
public static void keyReleased(int key, char c){
    scenes[currentScene.ordinal()].keyReleased(key, c);
}

2 个答案:

答案 0 :(得分:8)

您需要区分对象。例如,您可能有:

SceneObject[] scenes = { new Loading(), new Menu() };

Class[] classes = { Loading.class, Menu.class };

从您的问题中不清楚您的意思,但希望这应该满足任何一种情况......请注意,您不能拥有通用数组,所以使用Class[]您可以&# 39; t指定每个类必须扩展SceneObject

编辑:现在我们获得了更多信息,听起来你已经知道了:

abstract class SceneObject {}

class Menu extends SceneObject {
    static void foo() {
    }

    static void bar() {
    }
}

class Loading extends SceneObject {
    static void foo() {
    }

    static void bar() {
    }
}

这里的两个foo方法完全不相关 - 您无法使用多态来调用它们,因为它们是静态方法。如果你想使用多态 - 即调用一个方法知道你想要调用哪个签名,但是实现依赖于调用的目标 - 你需要实例方法:

abstract class SceneObject {
    abstract void foo() {
    }

    abstract void foo() {
    }
}

class Menu extends SceneObject {
    @Override void foo() {
    }

    @Override void bar() {
    }
}

class Loading extends SceneObject {
    @Override void foo() {
    }

    @Override void bar() {
    }
}

然后你可以写:

SceneObject[] scenes = { new Loading(), new Menu() };

...

for (SceneObject scene : scenes) {
    scene.foo();
    scene.bar();
}

答案 1 :(得分:1)

除了创建扩展SceneObject的类数组之外,我们可以为这些对象创建一个容器:

//a container that holds only the classes of SceneObject
public class ClassBox<T extends SceneObject> {
    private Class<T> theClass;

    public ClassBox(Class<T> theClass) {
        this.theClass = theClass;
    }
    public Class getTheClass() { //'getClass()' method is reserved, we use a more unique name
        return theClass;
    }
}

//testing classes
abstract class SceneObject {}
class Loading extends SceneObject {}
class Menu extends SceneObject {}
class noExtends1 {}

//testing 
public void main() {
    ClassBox[] scenes = {new ClassBox<>(Loading.class), new ClassBox<>(Menu.class)}; //these classes extend SceneObject
//  ClassBox[] sceneserror = {new ClassBox<>(Loading.class), new ClassBox<>(noExtends1.class)}; //gives error because 2nd array elem is not extending, so I commented it

    Log.v("custom log.v call", String.valueOf(Loading.class));
    Log.v("custom log.v call", String.valueOf(Menu.class));
    Log.v("custom log.v call", String.valueOf(scenes[0].getTheClass()));
    Log.v("custom log.v call", String.valueOf(scenes[1].getTheClass()));

//result is:
//V/custom log.v call: class me2.iwanttobealeader.pie$Loading
//V/custom log.v call: class me2.iwanttobealeader.pie$Menu
//V/custom log.v call: class me2.iwanttobealeader.pie$Loading
//V/custom log.v call: class me2.iwanttobealeader.pie$Menu
}

这个答案的好处是可以指定多个继承标准,例如:

public class ClassBox<T extends Fragment & FR_DbRequests.SynchListener>
//now it must implement the abstract class SyncListener and extend Fragment

但实例化一个额外的对象,即包含该类的容器+变量,而不仅仅是包含该类的变量,也是一个缺点。