개체에 액세스하는 데 어려움을 겪고 있으며 다른 클래스의 메서드입니다. 내 문제를 설명하기 위해 몇 가지 코드를 작성했습니다.자바 : 클래스 B에서 클래스 A에서 생성 된 액세스 개체
참고 : 다음 코드는 컴파일 또는 실행 가능하지 않으며 단지 내 문제점을 설명하기위한 것입니다.
class MainClass {
public static void(String[] args) {
Run r = new Run();
}
}
class Run {
Run() {
Brand cola = new Brand("Coca Cola");
Brand pepsi = new Brand("Pepsi");
// Creates the container object "con1" and adds brands to container.
Container con1 = new Container();
con1.addToList(cola);
con1.addToList(pepsi);
}
}
class Brand {
// In this class I have a method which needs to accsess the con1 object
containing all the brands and I need to access the method
public void brandMethod() {
if(con1.methodExample) { **// Error here. Can't find "con1".**
System.out.println("Method example returned true.");
}
}
}
class Container {
// This class is a container-list containing all brands brands
public boolean methodExample(){
}
}
나는 브랜드 클래스에서 "con1"개체에 액세스하는 데 어려움을 겪고 있습니다. "con1"에 대한 액세스 권한을 얻으려면 어떻게해야합니까?
나는 당신의 정확한 질문 제목을 봤는데 이것을 발견했습니다 - http://stackoverflow.com/questions/10570393/access-object-created-in-one-class-into-another 당신은 적어도 이것을 할 것으로 예상됩니다. SO에 게시하기 전에 많은 연구. – djechlin
문제는 개체에 액세스하는 방법이 아니라 액세스하는 방법입니다. –
참고 [한 클래스에서 다른 클래스로 작성된 액세스 객체] (http://stackoverflow.com/questions/10570393/access-object-created-in-one-class-into-another/14885596#14885596) –