English 中文(简体)
Enum helper in a category (Java)
原标题:Enum helper in a class (Java)
  • 时间:2012-05-19 16:15:34
  •  标签:
  • java
  • enums

如果我有一个<条码>(Enum)作为“helper,则以任何方式提及这一条码。” Enum out of the category it s helping?

基本上,我就是:

class Account extends MyClass {
    HashMap<Property, String> property = new HashMap<Property, String>();
    public Account() {
    }

    public enum Property {
        USERID,
        PASSWORD;
    }
}

我想能够进入<代码>Property,在<代码>Account类别之外。

我之所以要这样做,是因为这是一个另一个类别中的下级<>m>,我希望能够在不提及独一无二的名称(即:不提及每一名称,即<代码>Accountproperty或Researchproperty-TaskProperty等)。

最佳回答

您的表象是公开的,因此你可以使用<条码>。 账户/编码类别

EDIT :

如果我看你需要的话,你愿意做这样的事情。

Property p = Account.Property.PASSWORD;
Property p1 = Product.Property.CODE;

产品所在地

public class Product extends MyClass{
    HashMap<Account.Property, String> property = new HashMap<>();
    public Product() {
    }

    public static enum Property {
        CODE,
        PRICE;
    }
}

页: 1

问题是,这两条线都需要进口,你可以进口两个名称相同的班级,因此唯一的解决办法就是这样做。

Account.Property p = Account.Property.PASSWORD;
Product.Property p1 = Product.Property.CODE;

我猜测,你ve忙处理<条码>instanceof的问题,以使用<条码>Property对每一类人使用权,因为没有任何办法扩大权!

问题回答

也许像以下一些东西(但这种检查没有类型):

import java.util.*;
abstract class MyClass {
    Map<Object,String> properties=new HashMap<Object,String>();
}
class Account extends MyClass {
    enum Property {
        userid,password
    }
    //static Set<Property> keys=EnumSet.allOf(Property.class);
}
class Research extends MyClass {
    enum Property {
        red,green;
    }
    static Set<Property> keys=EnumSet.allOf(Property.class);
}
public class So10666881 {
    public static void main(String[] args) {
        Account account=new Account();
        account.properties.put(Account.Property.userid,"user");
        account.properties.put(Account.Property.password,"pass");
        for(Account.Property property:Account.Property.values())
            System.out.println(property+"="+account.properties.get(property));
    }
}

仅宣布 en为公共最高阶层(在自己的档案中)





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...