简体   繁体   English

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

[英]Java reference inner class attributes through outer class object

I'm having the problem my title describes. 我遇到标题所描述的问题。 I have an outer class called GAINEntities with an inner class in it called Entities. 我有一个名为GAINEntities的外部类,其中有一个称为Entities的内部类。 My goal is to reference the attributes of the inner class through objects of the outer class. 我的目标是通过外部类的对象引用内部类的属性。 I have a function readGainEntities(String inputUrl) which returns a Vector. 我有一个函数readGainEntities(String inputUrl)返回一个向量。 Thus, in my method i call readGainEntities method and set its content to a new Vector 因此,在我的方法中,我调用readGainEntities方法并将其内容设置为新的Vector

Example Code: 示例代码:

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;
}

Then in my Translate method: 然后在我的翻译方法中:

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.

So,is there something wrong with my code as im still unsure how to reference the Entities attributes through the GAINEntities objects which readGainEntities returns 所以,我的代码有什么问题吗,因为我仍然不确定如何通过readGainEntities返回的GAINEntities对象引用Entities属性

Generally you can reference the attributes of the Inner class outside the Outer class only when you have an Object of the Outer class: 通常,只有当您拥有Outer类的对象时,才可以在Outer类之外引用Inner类的属性:

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

provided that the doStuff() method is public. 前提是doStuff()方法是公共的。

If the Inner class is static then you can reference it as: 如果Inner类是static则可以将其引用为:

new Outer.Inner().doStuff();

In your example you do not show the classes involved. 在您的示例中,您没有显示所涉及的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM