我有一个名为Person的对象(姓名,年龄) 此对象使用本地文件中的值填充。 但我希望通过应用程序保持此对象可见,因为有一个用于向其添加新值的GUI。 我试图把这样的界面放在一边但不起作用
public interface MyInterface{
public List<Person> myPersonObj = new ArrayList<Person>();
}
并在MyInterface.myPersonObj
等应用中调用它
有人能帮我吗?
感谢
答案 0 :(得分:1)
您必须声明变量static,例如
public static (final) List<Person> persons = new ArrayList();
有关详情,请参阅Singelton Pattern
答案 1 :(得分:0)
因为它在界面中会自动为final
(只读),所以你可能想把它放在一个类中:
public class AnyClass{
public static List<Person> myPersonObj = new ArrayList<Person>();
}
然后你可以这样做:
AnyClass.myPersonObj
访问它。
答案 2 :(得分:0)
在您填充它的类中列出 static 列表。
class Populate{
public static List<Person> myPersonObj;
public static void setMyPersonObj(List<Person> inst){
myPersonObj =inst;
}
public static List<Person> getMyPersonObj(){
if(MyPersonObj!=null){
return myPersonObj;
}
else {
return new ArrayList<Person>();
}
}
//populate it
}
class SomeOther {
public static void someMethod(){
Populate.getMyPersonObj();
}
}