Java ->C# 创建抽象类的匿名实例 [英] Java -> C# Creating Anonymous Instance of Abstract Class
问题描述
出于培训目的,我正在遵循为 Java 编写的教程,到目前为止我已成功将其翻译"成 C#,但是,我现在面临一个问题,我真的不知道如何解决它.我能找到的最接近(?)我的问题的可能答案是 这个问题. 虽然我现在在理解委托和 Lamba 表达式方面有问题.无论如何,这里是Java中的相关代码:
for training purposes I am following along a Tutorial written for Java, which I successfully 'translated' into C# so far, however, I am facing now a problem for which I don't really have a clue how to solve it. The closest(?) possible answer to my problem I could find was this question. Though I have problems understanding delegates and lamba expressions for now. Anyways, here the relevant Code in Java:
public abstract class LevelUpOption {
private String name;
public String name() { return name; }
public LevelUpOption(String name){
this.name = name;
}
public abstract void invoke(Creature creature);
}
还有另一个类:
public class LevelUpController {
private static LevelUpOption[] options = new LevelUpOption[]{
new LevelUpOption("Increased hit points"){
public void invoke(Creature creature) { creature.gainMaxHp(); }
},
new LevelUpOption("Increased attack value"){
public void invoke(Creature creature) { creature.gainAttackValue(); }
},
new LevelUpOption("Increased defense value"){
public void invoke(Creature creature) { creature.gainDefenseValue(); }
},
new LevelUpOption("Increased vision"){
public void invoke(Creature creature) { creature.gainVision(); }
}
};
public void autoLevelUp(Creature creature){
options[(int)(Math.random() * options.length)].invoke(creature);
}
public List<String> getLevelUpOptions(){
List<String> names = new ArrayList<String>();
for (LevelUpOption option : options){
names.add(option.name());
}
return names;
}
public LevelUpOption getLevelUpOption(String name){
for (LevelUpOption option : options){
if (option.name().equals(name))
return option;
}
return null;
}
}
我遇到的问题是这部分:
The problem I have is with this part:
private static LevelUpOption[] options = new LevelUpOption[]{
new LevelUpOption("Increased hit points"){
public void invoke(Creature creature) { creature.gainMaxHp(); }
},
new LevelUpOption("Increased attack value"){
public void invoke(Creature creature) { creature.gainAttackValue(); }
},
new LevelUpOption("Increased defense value"){
public void invoke(Creature creature) { creature.gainDefenseValue(); }
},
new LevelUpOption("Increased vision"){
public void invoke(Creature creature) { creature.gainVision(); }
}
};
虽然很容易理解它在做什么,但我不知道如何在 C# 中编写相对相似的内容.我可以用非常简单的方式来解决它,比如 if 或 switch case,但我想保持它平滑到原始状态.
While easy to understand in terms what it is doing I have no clue how to write that relatively similiar in C#. I could work around it in very simplistic ways like with if or switch cases but I would like to keep it smooth to the original.
推荐答案
C# 中没有匿名类,但是您有两种方法可以达到相同的结果:
There are no anonymous classes in C#, but you have two ways of achieving the same result:
- 创建私有的、嵌套的、命名的类,并在数组初始值设定项中引用它们,或
- 让构造函数为您计划覆盖的每个抽象方法接受一个委托.
第一种方法不言自明,但代码会更长一些.命名的类应该没问题,因为它们对于公开可见的类的实现来说是私有的.
The first approach is self-explanatory, but the code would be quite a bit longer. The named classes should be OK, because they are private to the implementation of your publicly visible class.
第二种方法可能如下所示:
The second approach could look as follows:
public class LevelUpOption {
private String name;
public String name() { return name; }
public LevelUpOption(String name, Action<Creature> invoke){
this.name = name;
this.invoke = invoke;
}
public readonly Action<Creature> invoke;
}
现在你可以像这样初始化你的数组:
Now you can initialize your array like this:
private static LevelUpOption[] options = new [] {
new LevelUpOption("Increased hit points", c => c.gainMaxHp() ),
new LevelUpOption("Increased attack value", c => c.gainAttackValue()),
...
};
由于invoke
是一个委托,调用它的语法是一样的:
Since invoke
is a delegate, the syntax of calling it is the same:
options[i].invoke(myCreature);
这篇关于Java ->C# 创建抽象类的匿名实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!