Экземпляр Java Enum, содержащий Массив другого Enum (Континент, Страна): возвращает только нули в Массиве - PullRequest
0 голосов
/ 04 марта 2011

Когда я вызываю getCountries () из Enum Continent.EUROPE, я получаю список правильного размера, но содержащиеся в нем значения (должны быть Enums типа Country) все являются «нулевыми». Я неправильно выполняю инициализацию?

Мои перечисления:

public enum Continent {
    ANY, AFRICA, ANTARCTICA, ASIA, AUSTRALIA_OCEANIA, EUROPE, NORTH_AMERICA, SOUTH_AMERICA;

    private Country[] countries;

    public void setCountries(Country[] countries) {
    this.countries = countries;
    }

    public Country[] getCountries() {
    return this.countries;
    }

    static {
    EUROPE.setCountries(new Country[] { Country.ALAND_ISLANDS,
        Country.ALBANIA, Country.ANDORRA, Country.AUSTRIA,
        Country.BELARUS, Country.BELGIUM, Country.BOSNIA_HERZEGOVINA,
        Country.BULGARIA, Country.CROATIA, Country.CZECH_REPUBLIC,
        Country.DENMARK, Country.ESTONIA, Country.FAROE_ISLANDS,
        Country.FINLAND, Country.FRANCE, Country.GERMANY,
        Country.GIBRALTAR, Country.GREECE, Country.GUERNSEY,
        Country.HUNGARY, Country.ICELAND, Country.IRELAND,
        Country.ISLE_OF_MAN, Country.ITALY, Country.JERSEY,
        Country.KOSOVO, Country.LATVIA, Country.LIECHTENSTEIN,
        Country.LITHUANIA, Country.LUXEMBOURG, Country.MACEDONIA,
        Country.MALTA, Country.MOLDOVA, Country.MONACO,
        Country.MONTENEGRO, Country.NETHERLANDS, Country.NORWAY,
        Country.POLAND, Country.PORTUGAL, Country.ROMANIA,
        Country.RUSSIAN_FEDERATION, Country.SAN_MARINO, Country.SERBIA,
        Country.SLOVAKIA, Country.SLOVENIA, Country.SPAIN,
        Country.SVALBARD_AND_JAN_MAYEN, Country.SWEDEN,
        Country.SWITZERLAND, Country.UKRAINE, Country.UNITED_KINGDOM,
        Country.VATICAN_CITY });
    }
}

Моя страна Enum:

public enum Country {
    ALAND_ISLANDS("AX", Continent.EUROPE, new City[] {} ),
    ALBANIA("AL", Continent.EUROPE, new City[] {} ),
    ANDORRA("AD", Continent.EUROPE, new City[] {} ),
    ...
    private Continent continent;
    private String countryCode;
    private City[] cities;

    private Country(String countryCode, Continent continent, City[] cities ) {
        this.continent = continent;
        this.countryCode = countryCode;
        this.cities = cities;
    }

    public Continent getContinent() {
        return this.continent;
    }

    public City[] getCities() {
        return this.cities;
    }

    /**
     * @return the country code
     */
    public String getCountryCode() {
        return countryCode;
    }
}

Continent.EUROPE.getCountries() returns (in Eclipse debug view):
[null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]

правильное количество стран, но все ноль; - (

Есть намеки?

...