我这边有点问题.我正在使用泛型类,但我不知道从UI面板中选择之前的类型.如何在知道对象之前声明我的对象?所以这是样本
public class UI
{
Agent myAgent;
public UI()
{
}
public void Initialize()
{
if (textBox1.Text == "LabAgent")
myAgent = new Agent<LabState, LabAction>();
else
myAgent = new Agent<FieldState, FieldAction>();
}
}
Run Code Online (Sandbox Code Playgroud)
Agent类是一个简单的泛型类.我怎么能做这样的事情,但实际工作?如何在不知道类型的情况下定义myAgent?注意:我使用的是C#,而不是Java.编辑1:
public class Agent<S,A>
where S : AState
where A : AAction
{
private List<S> states;
private List<A> actions;
private Dictionary<KeyValuePair<S, A>, float> Q;
private S stareCurenta;
public void Initializare()
{
foreach (S s in states)
{
foreach (A a in actions)
{
Q.Add(new KeyValuePair<S, A>(s, a), 32000); …
Run Code Online (Sandbox Code Playgroud)