这是我的代码:
public class One
{
//Loots is a class
public static Loots[] lootsList = new Loots[] { };
}
public class Two
{
Loots MyLoots = new Loots();
// How to add in THIS class, MyLoots to lootsList?
}
请帮帮我 我不想使用List!
答案 0 :(得分:0)
访问静态变量和另一个类的方法
ClassName.StaticVariable;
或强>
ClassName.StaticMethod;
注意: - 静态变量和方法在整个应用程序中都处于活动状态。
使用此
public class One
{
public static Loots[] lootsList = new Loots[1];
}
public class Two
{
Loots MyLoots = new Loots();
// How to add in THIS class, MyLoots to lootsList?
One.Loots[0]=MyLoots; //use this to access array of objects
}
享受编码.........
答案 1 :(得分:0)
public class One{
public List<Loots> lootsList = new ArrayList<Loots>();
public void someVoid(){
new Two(this);
}
}
public class Two
{
One one;
public Two(One one){
this.one = one:
}
Loots MyLoots = new Loots();
// How to add in THIS class, MyLoots to lootsList?
public void someVoid(){
one.lootsList.add(MyLoots);
}
}
这是一种更好的方法,因为ArrayList可以根据需要保存任意数量的对象,而不需要通过列表的初始化来定义。
通过像你一样定义它,数组中必须有特定数量的Loots max。
替代:
public static Loots[] lootsList = new Loots[20];
示例:
public class One{
public static Loots[] lootsList = new Loots[20];
public void someVoid(){
new Two(this);
}
}
public class Two
{
One one;
public Two(One one){
this.one = one:
}
Loots MyLoots = new Loots();
// How to add in THIS class, MyLoots to lootsList?
public void someVoid(){
one.lootsList[0].add(MyLoots);
}
}