프로그래밍에 익숙하지 않아서 내가 묻는 것이 실제로 가능한지 확실하지 않습니다.클래스의 인스턴스를 다른 클래스의 배열 목록에 인스턴스 추가시 자동으로 추가합니다.
나는 하나의 클래스 인 Man
을 가지고 있는데,이 클래스는 클래스의 매개 변수를 포함하고 있으며 객체 특정 매개 변수를 찾기위한 몇 가지 get 메소드를 가지고있다.
나는 ArrayList
이 Man
인 다른 클래스 인 Group
을 갖고 있습니다.
그런데 차이가 있다면 bluej 환경을 사용하는 법을 배우고 있습니다.
다음은 Man
클래스의 관련 섹션입니다.
import java.util.ArrayList;
/**
*This class will store information about man, including the height, weight, age and year of birth
*of individual men.
*/
public class Man
{
// stats.
public int Height;
public int Weight;
public int Age;
public int Year;
/**
* The user will input the stats about the man in input parameters.
*/
public Man(int Height, int Weight, int Age, int Year)
{
this.Height = Height;
this.Weight = Weight;
this.Age = Age;
this.Year = Year;
}
아래의 통계에는 몇 가지 방법이 있습니다.
다른 클래스 인 Group
에서는 위의 클래스의 모든 인스턴스를 ArrayList
으로 보내겠습니다. 지금까지 내가 가진 :
import java.util.ArrayList;
/**
*Record all men.
*/
public class Group
{
public ArrayList <Man> AllMen;
/**
* Constructor for objects of class Group
*/
public Group()
{
AllMen = new ArrayList <Man>();
}
public void AddMan(int Height, int Weight, int Age, int Year) {
AllMen.add(new Man(Height, Weight, Age, Year,)); //trying to get it to list all men created.
}
}
두 사람은 나는 그들이 생각했던 방식으로 연결하는 것 같지 않습니다. 내가 뭘 놓치고 있니?
정확한 문제는 무엇입니까? 당신은 무엇을 기대했고 무엇을 얻었습니까? –
남자 오브젝트를 만들 때 자동으로 AllMen 배열 목록으로 이동하지만 Group 클래스의 Man을 인식하지 못한다고 기대하고있었습니다. – Finlay
그럼'AddMan' 메소드에서 잘하고있는 것처럼 보입니다. 나는 자바 개발자는 아니지만'Group'이'Man'을 보지 않는다면 두 클래스가 같은 패키지에 있지 않기 때문일 수 있습니다. –