0

我遇到了我的标题描述的问题。我有一个名为 GAINEntities 的外部类,其中有一个名为 Entities 的内部类。我的目标是通过外部类的对象来引用内部类的属性。我有一个函数 readGainEntities(String inputUrl) 返回一个向量。因此,在我的方法中,我调用 readGainEntities 方法并将其内容设置为新的 Vector

示例代码:

protected static Vector<LinkedHashTreeMap> getGainEntities(String inputUrl) {

    URL url = null;
    try {
        url = new URL(inputUrl);

    } catch (MalformedURLException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }
    URLConnection yc = null;
    try {
        yc = url.openConnection();
    } catch (IOException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }
    String userpass = "" + ":" + "";
    String basicAuth = "Basic "
            + new String(new Base64().encode(userpass.getBytes()));
    yc.setRequestProperty("Authorization", basicAuth);
    BufferedReader in = null;
    try {
        in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Gson gson = new Gson();
    List<LinkedHashTreeMap> items = null;
    try {
        items = gson.fromJson(in.readLine(),
                new TypeToken<List<LinkedHashTreeMap>>() {
                }.getType());
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    Vector<LinkedHashTreeMap> sessions = new Vector<LinkedHashTreeMap>();
    for (int i = 0; i < items.size(); i++) {
        sessions.add(items.get(i));
    }

    return sessions;
}



public static Vector<GAINEntities> readGainentities(String inputUrl) {
    Vector<GAINEntities> exp = new Vector<GAINEntities>();
    Vector<LinkedHashTreeMap> sessions = getGainEntities(inputUrl);
    Iterator it = sessions.iterator();
    while (it.hasNext()) {
        LinkedHashTreeMap next = (LinkedHashTreeMap) it.next();
        GAINEntities input = new GAINEntities();
        input.setObjectID((String) next.get("objectId"));
        input.setSubobjectID((String) next.get("subobjectId"));

        LinkedHashTreeMap<String, String> lhmt = (LinkedHashTreeMap<String, String>) next
                .get("attributes");

        data.GAINEntities.Attributes atts = input.new Attributes();
        atts.setAttributeStart(Double.parseDouble(String.valueOf(lhmt
                .get("start"))));
        atts.setAttributeEnd(Double.parseDouble(String.valueOf(lhmt
                .get("end"))));

        input.setAttributes(atts);
        input.setAccountID((String) next.get("accountId"));
        input.setID((String) next.get("_id"));
        input.setV(Double.parseDouble(String.valueOf(next.get("__v"))));
        ArrayList<LinkedHashTreeMap<String, String>> al = (ArrayList<LinkedHashTreeMap<String, String>>) next
                .get("entities");
        ArrayList<Entities> ents = new ArrayList<Entities>();

        for (int i = 0; i < al.size(); i++) {
            ents.add(input.new Entities(al.get(i).get("ntype"), al.get(i)
                    .get("source"), al.get(i).get("lod"), al.get(i).get(
                    "type"), al.get(i).get("label"), Double
                    .parseDouble(String
                            .valueOf(al.get(i).get("confidence"))),
                    Double.parseDouble(String.valueOf(al.get(i).get(
                            "relevance")))));
        }
        input.setEntities(ents);

        exp.add(input);
        // System.out.println(input);
        // System.out.println(input);
    }

    return exp;
}

然后在我的翻译方法中:

public static String translateGAINEntities(String url) {
    LogicFactory.initialize();
    Vector<GAINEntities> exp = readGainEntities.readGainentities(url);
for (int i = 0; i < exp.size(); i++) {
        LogicFactory.initialize();
        GAINEntities gexp = exp.get(i);
System.out.println("HEREEE  \t" + gexp.getEntities()); <-- returns empty.

那么,我的代码有问题吗,因为我仍然不确定如何通过 readGainEntities 返回的 GAINEntities 对象引用实体属性

4

1 回答 1

1

一般只有当你有类的对象时才能在Inner类外引用类的属性:OuterOuter

new Outer().new Inner().doStuff();

前提是doStuff()方法是公开的。

如果Inner类是,static那么您可以将其引用为:

new Outer.Inner().doStuff();

在您的示例中,您没有显示所涉及的类。

于 2013-09-03T08:30:33.313 回答