Java通过外部类对象引用内部类属性

时间:2013-09-03 08:18:54

标签: java reference inner-classes

我遇到了我的标题所描述的问题。我有一个名为GAINEntities的外部类,其中包含一个名为Entities的内部类。我的目标是通过外部类的对象引用内部类的属性。我有一个函数readGainEntities(String inputUrl),它返回一个Vector。因此,在我的方法中,我调用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.

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

1 个答案:

答案 0 :(得分:1)

通常,只有当您拥有Inner类的Object时,才能在Outer类之外引用Outer类的属性:

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

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

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

new Outer.Inner().doStuff();

在您的示例中,您不会显示所涉及的类。