2016-07-09 5 views

답변

0

다른 클래스의 static 변수 및 방법을 액세스

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 
    } 

이는 ArrayList를 사용하면 많은 객체를 저장할 수 있기 때문에 그것을 할 수있는 더 좋은 방법입니다 .........

+0

고마워요 :) –

+0

downVote please comment 왜? ...... – sushildlh

+0

아니 .. 나 아니야. –

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

} 

코딩을 즐길 수 그것의 필요없이 목록의 초기화에 의해 정의됩니다.

당신이했던 것처럼 그것을 정의하면 배열에 특정 양의 Loots가 있어야합니다.

대안 :

public static Loots[] lootsList = new Loots[20]; 

예 : 대신 ArrayList`

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

} 
+0

목록을 사용하고 싶지 않습니다. –

+0

arraylist를 사용하고 싶지 않다면 배열에서 Loots max의 양을 정의해야합니다 – Zoe

관련 문제