Java-> C#创建抽象类的匿名实例 [英] Java -> C# Creating Anonymous Instance of Abstract Class

查看:102
本文介绍了Java-> C#创建抽象类的匿名实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于培训目的,我遵循的是为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;
    }
}

我的问题在于这部分:

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的非常简单的方法来解决它,但我想使其保持 smooth 到原始状态。

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.

第二种方法如下:

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屋!

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