使用枚举内部的接口

时间:2016-04-22 14:07:51

标签: java interface enums

我搞乱了枚举和界面,但我似乎无法添加' moneyType'我已经实现了enum的界面,我一直无法找到解决方案。 基本上:moneyType是红色下划线,我无法弄清楚我是如何做到的!

"""
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/geopy/point.py", line 123, in __new__
    seq = iter(arg)
TypeError: 'numpy.int64' object is not iterable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.4/multiprocessing/pool.py", line 119, in worker
    result = (True, func(*args, **kwds))
  File "/usr/lib/python3.4/multiprocessing/pool.py", line 44, in mapstar
    return list(map(*args))
  File "gps.py", line 29, in calc_dist
    for grp, lst in df.groupby('co_nm').groups.items()
  File "gps.py", line 30, in <listcomp>
    for c in combinations(lst, 2)
  File "/usr/local/lib/python3.4/dist-packages/geopy/distance.py", line 322, in __init__
    super(vincenty, self).__init__(*args, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/geopy/distance.py", line 115, in __init__
    kilometers += self.measure(a, b)
  File "/usr/local/lib/python3.4/dist-packages/geopy/distance.py", line 342, in measure
    a, b = Point(a), Point(b)
  File "/usr/local/lib/python3.4/dist-packages/geopy/point.py", line 126, in __new__
    "Failed to create Point instance from %r." % (arg,)
TypeError: Failed to create Point instance from 8.
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "gps.py", line 38, in <module>
    pool.map(calc_dist, ['lat', 'lon'])
  File "/usr/lib/python3.4/multiprocessing/pool.py", line 260, in map
    return self._map_async(func, iterable, mapstar, chunksize).get()
  File "/usr/lib/python3.4/multiprocessing/pool.py", line 599, in get
    raise self._value
TypeError: Failed to create Point instance from 8.

我还没有使用枚举和接口。

2 个答案:

答案 0 :(得分:2)

您不需要在枚举列表声明中传递moneyType,或者将其放在构造函数中,只需将其保留。每个枚举实例的原因是moneyType

interface MoneyType {
    public int getMoney();
    public String getMoneyName();
    public String getMessage();
}

public enum MoneyTypes implements MoneyType {
    DOLLAR(15, "Dollar"), EURO(15, "Dollar"), FRANK(15, "Dollar"), MARK(15, "Dollar"), POUND(15, "Dollar");

    private final int amount;
    private final String moneyName;

    MoneyTypes(int amount, String name) {
        this.amount = amount;
        this.moneyName = name;
    }

    @Override
    public int getMoney() {
        return this.amount;
    }

    @Override
    public String getMoneyName() {
        // moneyName is assigned the value of name
        // You were returning name();
        // Two different values, decide which you want to return.
        return this.moneyName;
    }

    @Override
    public String getMessage() {
        return "got the message";
    }
}

现在你可以做到:

MoneyType type = MoneyTypes.DOLLAR;

请注意,我更改了接口和枚举声明的名称以符合Java命名标准,特别是以大写字母开头。我还将枚举的字段修改为final,因此在构造之后无法分配它们。

答案 1 :(得分:1)

这可能吗?

package Se.lucas.Main;

public enum moneyTypes implements moneyType {

DOLLAR(15, "Dollar"),
 EURO(15, "Dollar"), 
  FRANK(15, "Dollar"),
   MARK(15, "Dollar"),
    POUND(15, "Dollar");

private int amount;
private String moneyName;

moneyTypes(int amount, String moneyName) {
     this.amount = amount;
     this.moneyName= moneyName;

}


@Override
public int getMoney() {
    return this.amount;

}

@Override
public String getMoneyName() {
    return this.moneyName;
}

@Override
public String getMessage() {
    return "got the message";
}

注意:

  1. this错误的一面
  2. 您使用的this.name()会在DOLLAR中返回getMoneyName()而不是(我认为)this.moneyName
  3. 我将name重命名为moneyName,与枚举name()
  4. 没有冲突

    最后一点:在这样的枚举上使用接口看起来有点太多了,你真的需要那个界面吗?您可以在没有任何接口的情况下向枚举添加方法。