我有一本字典,其中有一个字符串作为键,一个类作为值,其中包含我在游戏中拥有的“实体”列表。
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);
...
}
这是一个示例层次结构。
Entity (abstract)
^
Mobile (abstract)
^
BaseCreature (abstract)
^
NpcSomething
-Entity包含称为的方法"public void Input(String args)"
,可以在其他实体中重新定义。
-当我调用Input("x")
NpcSomething时,它应该执行一条super(arg)
从其自己的类到Entity的类的链。
-上面所有这些类都有一个允许字符串作为参数的构造函数。
我有一个用于创建实体新实例的独立静态方法,如下所示:
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(); }
}
}
我的问题。
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)"
。
快速问题的答案。
-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继承的...
我没有找到如何做我想做的好的实际例子。
我在文档中找不到任何特别的东西。
欢迎任何帮助。
假设您Entity ent
从newInstance()
方法获得实例。这是您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] 删除。
我来说两句