是否有可能模仿C#这个Java代码枚举 [英] Is it possible to mimic this java enum code in c#

查看:129
本文介绍了是否有可能模仿C#这个Java代码枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以复制提供在一个枚举的抽象方法内心的枚举常量必须重写,并提供功能,此功能?

Is it possible to copy this functionality of offering an abstract method in an enum that inner enum constants must override and provide functionality to?

public enum Logic {
    PAY_DAY {
        @Override
        public void acceptPlayer(Player player) {
            // Perform logic
        }
    },
    COLLECT_CASH {
        @Override
        public void acceptPlayer(Player player) {
            // Perform logic
        }
    }
    ,
    ETC_ETC {
        @Override
        public void acceptPlayer(Player player) {
            // Perform logic
        }
    };

    public abstract void acceptPlayer(Player player);
}

如果它不能::
你能提供,我可以实现很多特殊的逻辑以类似的方式的方式?

If it can't:: Could you provide a way which I can implement lots of specific logic in a similar manner?

:我知道,在C#中枚举是不是真的对象就像他们在Java中,但我希望执行相似的逻辑。

Edit :: I know that enums in C# are not really 'objects' like they are in Java, but I wish to perform similar logic.

:为了澄清,我不想逻辑的每个特定位提供具体的类。 IE浏览器,创建接口acceptPlayer和创造了许多新的类不appropiate

Edit :: To clarify, I do not want to provide concrete classes for each specific bit of logic. IE, creating an interface acceptPlayer and creating many new classes is not appropiate

推荐答案

下面是一个选项 - 不使用枚举,但类似的东西-ish ...

Here's one option - not using enums, but something similar-ish...

public abstract class Logic
{
    public static readonly Logic PayDay = new PayDayImpl();
    public static readonly Logic CollectCash = new CollectCashImpl();
    public static readonly Logic EtcEtc = new EtcEtcImpl();

    // Prevent other classes from subclassing
    private Logic() {}

    public abstract void AcceptPlayer(Player player);

    private class PayDayImpl : Logic
    {
        public override void AcceptPlayer(Player player)
        {
            // Perform logic
        }
    }

    private class CollectCashImpl : Logic
    {
        public override void AcceptPlayer(Player player)
        {
            // Perform logic
        }
    }

    private class EtcEtcImpl : Logic
    {
        public override void AcceptPlayer(Player player)
        {
            // Perform logic
        }
    }
}

您的你不想为逻辑的每一位具体的类 - 但基本上你会用Java反正做什么,它只是该类会从你们稍微隐藏

You say that you don't want to provide a concrete class for each bit of logic - but that's basically what you'd be doing in Java anyway, it's just that the class would be slightly hidden from you.

下面是使用代理的不同行为的另一种方法:

Here's an alternative approach using delegates for the varying behaviour:

public sealed class Logic
{
    public static readonly Logic PayDay = new Logic(PayDayAccept);
    public static readonly Logic CollectCash = new Logic(CollectCashAccept);
    public static readonly Logic EtcEtc = new Logic(player => {
        // An alternative using lambdas...
    });

    private readonly Action<Player> accept;

    private Logic(Action<Player> accept)
    {
        this.accept = accept;
    }

    public void AcceptPlayer(Player player)
    {
        accept(player);
    }

    private static void PayDayAccept(Player player)
    {
        // Logic here
    }

    private static void CollectCashAccept(Player player)
    {
        // Logic here
    }
}

在这两种情况下,你仍然可以得到一组固定的值 - 但你不能对它们进行切换。你可能有一个单独的真正的枚举,但是这将是一个有点乱。

In both cases, you still get a fixed set of values - but you won't be able to switch on them. You could potentially have a separate "real" enum, but that would be a bit messy.

这篇关于是否有可能模仿C#这个Java代码枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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