我可以在VB.NET中给一个枚举一个属性(像我在Java中可以做到的)? [英] Can I give an enum an attribute in VB.NET (like I can do in Java)?

查看:325
本文介绍了我可以在VB.NET中给一个枚举一个属性(像我在Java中可以做到的)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,我可以这样做:

In Java I can do something like this:

enum Country {
    IRELAND("Europe"),
    FRANCE("Europe"),
    NIGERIA("Africa"),
    THAILAND("Asia");

    private String continent;

    Country(String continent) {
        this.continent = continent;
    }

    public String getContinent() {
        return continent;
    }
}

这允许我做一些类似的事情:

which allows me to do something like:

Country country1 = getCountryFromSomewhere();
Country country2 = Country.FRANCE;
System.out.print("country1 is in " + country1.getContinent());
System.out.print("country2 is in " + country2.getContinent());

可以在VB.NET中做同样的事情,即将大陆属性添加到国家枚举?

Is it possible to do the same thing in VB.NET i.e. add the continent attribute to the country enum?

推荐答案

我使用这个解决方案:

声明枚举:

Private Enum Country
    IRELAND
    FRANCE
    THAILAND
End Enum

声明和初始化字典(也称为地图):

Declare and initialise Dictionary (aka a map):

Dim countryContinentMap As IDictionary(Of Country, String) = New Dictionary(Of Country, String)
countryContinentMap.add(Country.IRELAND, "Europe")
countryContinentMap.add(Country.FRANCE, "Europe")
countryContinentMap.add(Country.THAILAND, "Asia")

这让我得到这样的大陆:

which allows me to get the continent like this:

Dim franceContinent As String = countryContinentMap(Country.FRANCE)

这篇关于我可以在VB.NET中给一个枚举一个属性(像我在Java中可以做到的)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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