[英]Java reference inner class attributes through outer class object
我遇到标题所描述的问题。 我有一个名为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对象引用Entities属性
通常,只有当您拥有Outer
类的对象时,才可以在Outer
类之外引用Inner
类的属性:
new Outer().new Inner().doStuff();
前提是doStuff()
方法是公共的。
如果Inner
类是static
则可以将其引用为:
new Outer.Inner().doStuff();
在您的示例中,您没有显示所涉及的类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.