gpt4 book ai didi

Java -> C# 创建抽象类的匿名实例

转载 作者:太空宇宙 更新时间:2023-11-03 21:29:22 24 4
gpt4 key购买 nike

出于培训目的,我正在学习为 Java 编写的教程,到目前为止我已成功将其“翻译”成 C#,但是,我现在面临一个问题,我真的不知道如何解决它。我能找到的最接近(?)可能的答案是 this question.虽然我现在在理解委托(delegate)和兰巴表达式方面有问题。无论如何,这里是 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;
}
}

我的问题是这部分:

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,但我想保持它平滑到原来的样子。

最佳答案

C# 中没有匿名类,但是有两种方法可以达到相同的结果:

  • 创建私有(private)的、嵌套的、命名的类,并在数组初始化器中引用它们,或者
  • 让构造函数为您计划重写的每个抽象方法接受一个委托(delegate)。

第一种方法不言自明,但代码会长很多。命名的类应该没问题,因为它们对于您公开可见的类的实现是私有(private)的。

第二种方法如下所示:

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;
}

现在你可以像这样初始化你的数组:

private static LevelUpOption[] options = new [] {
new LevelUpOption("Increased hit points", c => c.gainMaxHp() ),
new LevelUpOption("Increased attack value", c => c.gainAttackValue()),
...
};

因为 invoke 是一个委托(delegate),所以调用它的语法是一样的:

options[i].invoke(myCreature);

关于Java -> C# 创建抽象类的匿名实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25037186/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com