Java枚举支持方法吗?但在c#中不行?

发布于 2024-12-12 08:52:50 字数 647 浏览 0 评论 0原文

我正在查看在java中执行单例的正确方法的代码: 在 Java 中实现单例模式的有效方法是什么?

我有点困惑,如何向枚举添加方法?

 public enum Elvis {
       INSTANCE;
       private final String[] favoriteSongs =
           { "Hound Dog", "Heartbreak Hotel" };
       public void printFavorites() {
           System.out.println(Arrays.toString(favoriteSongs));
       }
   }

上面代码中的枚举对我来说甚至没有意义,你有这个符号:

INSTANCE;

那怎么是正确的行呢?

我来自 c#,这种语法让我很好奇,我希望有人能够解释或理解上述内容。

我想这意味着java对枚举有更纯粹的想法,因为它可以有行为?

I'm looking at this code of the correct way to do a singleton in java: What is an efficient way to implement a singleton pattern in Java?

I'm a little confused, how do you add a method to a enumeration?

 public enum Elvis {
       INSTANCE;
       private final String[] favoriteSongs =
           { "Hound Dog", "Heartbreak Hotel" };
       public void printFavorites() {
           System.out.println(Arrays.toString(favoriteSongs));
       }
   }

And the enumeration in the code above, doesn't even make sense to me, you have the symbol:

INSTANCE;

how is that a correct line?

I'm coming from c#, and this syntax got me curious and I'm hoping someone can explain or make sense of the above.

I guess it means java has a more purer idea of an enumeration as it can have behaviour?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦明 2024-12-19 08:52:54

您可以使用扩展方法

enum Elvis
{
  Instance
}

static class ElvisExtension
{
  private static readonly string[] FavoriteSongs = { "Hound Dog", "Heartbreak Hotel" };

  public static void PrintFavorites(this Elvis singleton)
  {
    Console.WriteLine(string.Join(", ", FavoriteSongs));
  }
}

用法:

Elvis.Instance.PrintFavorites();

You could use an extension method

enum Elvis
{
  Instance
}

static class ElvisExtension
{
  private static readonly string[] FavoriteSongs = { "Hound Dog", "Heartbreak Hotel" };

  public static void PrintFavorites(this Elvis singleton)
  {
    Console.WriteLine(string.Join(", ", FavoriteSongs));
  }
}

Usage:

Elvis.Instance.PrintFavorites();
余厌 2024-12-19 08:52:53

给出一个稍微不同的答案,我认为您正在问这个问题,Java 代码中的 INSTANCE; 行与以下 C# 示例中的 Instance 行等效:

enum Elvis
{
    Instance
}

这是为 Java enum 创建的方式。为了进一步说明这个例子,Java 枚举可以是:

public enum Gender {
    MALE,
    FEMALE;
}

其中值类型是 Gender.MALEGender.FEMALE 相当于:

enum Gender
{
    Male,
    Female
}

在 C# 中。

如果我偏离了你的问题,我深表歉意。

To give a slightly different answer, and one that I think you're asking, the INSTANCE; line in the Java code is the equivalent line of Instance in the following C# example:

enum Elvis
{
    Instance
}

It's the way you create a value for a Java enum. To further the example a Java enum could be:

public enum Gender {
    MALE,
    FEMALE;
}

Where the value types are Gender.MALE and Gender.FEMALE which is equivalent to:

enum Gender
{
    Male,
    Female
}

in C#.

If I have veered far from your question I apologize.

北城挽邺 2024-12-19 08:52:52

Java 中的枚举实际上是一个类。隐式扩展 java.lang.Enum 的一种。您在 INSTANCE; 中看到的特殊语法是枚举常量的声明。这将用于创建可以在代码中引用的枚举类的实例。事实上,您甚至可以为枚举设置一个非默认构造函数,并在常量声明中使用它。示例:

public enum Test {

    INSTANCE("testing");

    private final String content;
    public Test(final String content) {
        super();
        this.content = content;
    }

}

将枚举视为真实对象具有某些优点。将一些逻辑放入枚举中很方便,但也不应该被滥用。不过,在我看来,通过枚举的单例模式是一个很好的解决方案。

An enum in Java is actually a class. One that implicitly extends java.lang.Enum. The special syntax you see at INSTANCE; is a declaration of an enum constant. This is going to be used to make an instance of your enum class which can be referred to in code. As a matter of fact, you can even have a non-default constructor for your enum and use it in the constant declaration. Example:

public enum Test {

    INSTANCE("testing");

    private final String content;
    public Test(final String content) {
        super();
        this.content = content;
    }

}

Treating enums as true objects has certain advantages. Putting some logic into enums is convenient, but shouldn't be abused either. The singleton pattern via enums is a nice solution in my opinion, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文