我正在努力访问一个对象及其来自另一个类的方法。我写了一些代码来说明我的问题
注意:以下代码不可编译或不可运行,只是为了解释我的问题。
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”?