我需要将TDSContent存储到指定String键的哈希图中。我需要做这样的事情。

public class Target {
    public Target() {
        final int a = 0x64c896;
        final int b = 0xc050be;
        final int c = 0xffc896;

        TDS = new HashMap<String, TDSContent>();

        TDSContent contentMA = new TDSContent(a, b, c, 10);
        TDSContent contentMB = new TDSContent(a, c, b, 10);
        TDSContent contentMC = new TDSContent(b, a, c, 10);
        TDSContent contentMD = new TDSContent(c, b, a, 10);
        TDSContent contentME = new TDSContent(c, a, b, 10);
        TDSContent contentMF = new TDSContent(b, c, a, 10);
        ... // and so on...

        TDS.put("Marker A", contentMA);
        TDS.put("Marker B", contentMB);
        TDS.put("Marker C", contentMC);
        TDS.put("Marker D", contentMD);
        TDS.put("Marker E", contentME);
        TDS.put("Marker F", contentMF);
            ... // and so on...
    }

    public int getCL(String key) {
        TDSContent tdc = TDS.get(key);

        if(tdc.equals(contentMA)) {
            return contentMA.getValue();
        } else if(tdc.equals(contentMB)) {
            return contentMB.getValue();
        } else if(tdc.equals(contentMC)) {
            return contentMC.getValue();
        } else if(tdc.equals(contentMD)) {
            return contentMD.getValue();
        } else if(tdc.equals(contentME)) {
            return contentME.getValue();
        } else if(tdc.equals(contentMF)) {
            return contentMF.getValue();
        } ...// and so on...

             else {
            return contentMD.getValue();
        }
    }
}


问题是,手动创建类TDSContent的对象将需要大量的工作。

我可以做这样的事情吗...:

public class Target {
    public Target() {
        final int a = 0x64c896;
        final int b = 0xc050be;
        final int c = 0xffc896;

        TDS = new HashMap<String, TDSContent>();

           // form: new TDSContent(CL, CM, CR, D);
        TDS.put("Marker A", new TDSContent(a, b, c, 10));
        TDS.put("Marker B", new TDSContent(a, c, b, 10));
                ... // and so on..
    }

    public int getCL(String key) {
           // this method gets the first parameter of the TDSContent
           // constructor (see comment above).
        return TDS.get(key).getValue();
    }

    public int getCM(String key) {
    ... // similar to getCL but returns the second parameter of the TDSContent
        // constructor (see comment above)


...在getCL()上并获取和TDSContent的实例化[?]值?

具体来说,如果我打电话给类似的人:

Target target = new Target();
int x1 = target.getCL("Marker A"); // I should get 0x64c896 here
int x2 = target.getCM("Marker A"); // I should get 0xc050be here


那可能吗?

最佳答案

当然,只要将您在映射中的getXXX()对象上调用的TDSContent方法与访问器匹配即可。假定您在每个构造函数参数的TDSContent上都有访问器。

public int getCL(String key) {
    return TDS.get(key).getCL();
}

public int getCM(String key) {
    return TDS.get(key).getCM();
}

public int getCR(String key) {
    return TDS.get(key).getCR();
}

public int getD(String key) {
    return TDS.get(key).getD();
}

09-04 03:30