我很努力地访问一个对象,它是来自另一个类的方法。 我写了一些代码来说明我的问题Java:在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(){
}
}
我很努力地从Brand类中访问“con1”对象。 我怎样才能访问“con1”?
我搜索了你的确切问题标题,发现这个 - http://stackoverflow.com/questions/10570393/access-object-created-in-one-class-into-another你至少应该这样做在发布到SO之前进行大量研究。 – djechlin 2013-02-14 16:35:13
问题不是如何访问对象,而是如何访问它的权利? – 2013-02-14 16:36:30
另请参见[在另一个类中创建的访问对象](http://stackoverflow.com/questions/10570393/access-object-created-in-one-class-into-another/14885596#14885596) – 2013-02-14 23:01:10