Java:动态加载相同类的多个版本 [英] Java: Dynamically Load Multiple Versions of Same Class

查看:154
本文介绍了Java:动态加载相同类的多个版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是加载一组类,可能所有类都放在同一个文件夹中.所有这些都实现相同的接口,并且是相同的类,然后在我的代码中,我希望能够在这些类上调用函数.

What I'd like to be able to do is to load set of classes, probably all in the same folder. All of which implement the same interface and are the same class, then in my code I'd like to be able to call functions on those classes.

推荐答案

根据您对我的问题的回答,您似乎想要定义一个游戏界面,然后插入任何数量的AI实现,可能是通过.properties配置的文件.这是API接口的相当标准的用法.

Based on your answer to my question, it seems you want to define a game interface and then plug in any number of AI implementations, probably configured from a .properties file. This is fairly standard use of an API interface.

您定义一个EngineInterface,提供一个接受游戏状态并返回移动的方法.然后定义多个都实现EngineInterface的类.您的驱动程序读取属性文件以获取实现类的名称,使用Class.forName()实例化它们并将它们存储在列表和/或映射中.然后,当驱动程序收到请求时,它将依次调用每个实现并跟踪结果.

You define an EngineInterface providing a method that accepts game state and returns the move. Then you define multiple classes that all implement EngineInterface. Your driver reads a property file to get the names of the implementation classes, instantiates them with Class.forName() and stores them in a list and/or map. Then when the driver gets requests it invokes each implementation in turn and keeps track of the results.

这篇关于Java:动态加载相同类的多个版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆