java-从动态转换的超类调用子类方法

Po0ka

1:

我有一本字典,其中有一个字符串作为键,一个类作为值,其中包含我在游戏中拥有的“实体”列表。

private static Map<String, Class> entitiesList = new HashMap<String, Class>();
public static void initEntitiesList()
{
    entitiesList.put("npc_something", NpcSomething.class);
    entitiesList.put("npc_thing", NpcThing.class);
    entitiesList.put("npc_stuff", NpcStuff.class);
    ...
}

2:

这是一个示例层次结构。

Entity (abstract)
^
Mobile (abstract)
^
BaseCreature (abstract)
^
NpcSomething

-Entity包含称为的方法"public void Input(String args)",可以在其他实体中重新定义。
-当我调用Input("x")NpcSomething时,它应该执行一条super(arg)从其自己的类到Entity的类链。
-上面所有这些类都有一个允许字符串作为参数的构造函数。

3:

我有一个用于创建实体新实例的独立静态方法,如下所示:

public static boolean createEntity(String entName, String args)
{
    Class<?> entClass = null;
    if ((entClass = entitiesList.get(entName)) != null)
    {
        Entity ent;
        try
        {
            ent = (Entity)entClass.getDeclaredConstructor(String.class).newInstance("");

            //this here failed.
            //Method method = entClass.getMethod("input", new Class[] { ent.getClass() });
            //method.invoke(ent, new Object[] {ent});
            //java.lang.NoSuchMethodException: entities.NpcSomething.input(entities.NpcSomething)

            //this here is really out of place, as i plan on having a hundred of entities and even more...
            //if (entClass.isInstance(NpcSomething.class))

            //i tried stuffs related to:
            //T t = entClass.cast(ent);
            //but i could not understand it at all even with documentation.

            //basically i want to cast ent to entClass to call Input.
            //right now, the line under calls Input on an Entity class, which is what i want to avoid.
            ent.Input("Stuffs");
        }
        catch (InstantiationException ex) { ex.printStackTrace(); }
        catch (IllegalAccessException ex) { ex.printStackTrace(); }
        catch (IllegalArgumentException ex) { ex.printStackTrace(); }
        catch (InvocationTargetException ex) { ex.printStackTrace(); }
        catch (NoSuchMethodException ex) { ex.printStackTrace(); }
        catch (SecurityException ex) { ex.printStackTrace(); }
    }
}

4:

我的问题。

EntCreator.createEntity("NpcSomething", "stuffs");
EntCreator.createEntity("NpcThing", "stuffs");
EntCreator.createEntity("NpcStuff", "stuffs");

我想调用Input();NpcSomething,
我想调用Input();NpcThing,
我想调用Input();NpcStuff。
然后,这3个对象将调用其各自的超类代码,依此类推,直到它们到达Entity。

那些被强制转换为Entity "ent = (Entity)entClass.getDec...",因为我拥有Mobile,还有Item以及其他继承Input的类。然后使用该实体,找到正确的子类,并调用该子类的Input。

短线问题:将“ NpcSomething”创建为“ Entity”,然后将该实体转换为“ NpcSomething的类”以调用方法"Input(args)"

5:

快速问题的答案。

-Q:为什么要这样做?
-A:要创建带有预创建参数的实体,例如:创建一个("NpcSomething", "health 20 healthmax 25")

-Q:为什么不使用instanceof?
-A:instanceof在那个静态类中,我将需要200多个,这将是不好的编程习惯。

-Q:为什么不在Entity本身中移动输入法?
-A:我有许多具有不同值的不同实体,例如:NpcThing,唯一的飞行移动设备将具有flyingSpeed,flyingEnergy ... ItemScroll,具有文本,textColor,textFont ...这些是我无法放置在Entity中的东西,正如它所需要的那样instanceof,这对不仅仅对ents来说是一个坏习惯。

-Q:是否要将NpcSomething强制转换为Entity?
-A:再读一遍。

-Q:您能否提供更多信息?
我也很想这样做。

-Q:为什么不将ent声明为NpcSomething?
-A:因为ent可能是ItemStuff,ModelHouse等,它们不是从Mobile或BaseCreature继承的...

6:

我没有找到如何做我想做的好的实际例子。
我在文档中找不到任何特别的东西。
欢迎任何帮助。

罗希特·贾恩(Rohit Jain)

假设您Entity entnewInstance()方法获得实例这是您input(String)为该实例调用方法的方式:

// See here, the .class argument passed is the type of parameter
// You're using `ent.getClass()` here. It won't work
Method method = ent.getMethod("input", String.class);

// then while invoking this method, you pass the argument:
// Call `invoke()` method of `Method` class
// First arg is the instance on which this method should be called
// Remaining arg is the argument to be passed to the method itself.
result = method.invoke(ent, args);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Java:从超类构造函数中的子类调用的方法

您如何在Java中从超类调用子类方法?

超类引用无法在Java中调用子类方法

Java继承:在超类中调用子类方法

从 Java 中超类类型的 ArrayList 调用子类方法

Java:强制子类重写超类的方法

从动态调用Java类获取返回值

如何确保某些方法从子类(Java)中的方法从抽象超类中调用

在Java中,“ this”在继承该方法的子类上调用的超类方法中表示什么?

Java继承:创建子类实例时如何调用超类方法?

Java:从子类调用超类的受保护方法-不可见?

Java多态性如何调用子类对象的超类方法

Java继承问题。在超类数组中调用子类方法不会编译

有没有更简单的方法可以在Java中的子类和超类的ArrayList上调用子类的方法

Java多态性-如何确定是否将调用超类vs子类方法以及超类变量vs子类变量?

从Java中的子类构造函数调用超类

Java继承-调用超类方法

继承调用超类方法java

Java:在超类方法签名中返回子类

Java在超类方法中返回不带通用参数的子类

在调用超类的构造函数之前,有什么方法可以在Java中初始化子类的成员变量?

java泛型-如何在知道子类类型和超类的情况下调用子重载方法

从动态调用的类动态调用方法

Java反射:从超类继承的类中调用GetDeclaredFields()方法

Java在不同的类加载器下从子类转换为超类

在Java中动态调用类方法?

从超类调用子类方法

Java的:使用接口从父类调用子类中的方法

在子类对象java上调用父类的方法