2012-05-13 84 views
-3

如果您想直接找到问题,请跳过本段。在实践中,我试图编写一个模拟经济的Java程序,并为此写了一个公司类。这个想法就是说,其中有十几个将他们的收入包括在一个正常的变量函数中,那就是经济。从另一个类访问实例变量

我写了一个单独的类来使用JFreeChart绘制公司的输出图。但是,我无法访问ArrayList,我从图表类中每年写入大量金钱。我明白最好的方法是使用getter,但这似乎不起作用,所以如果这是您的建议,请您举个例子吗?谢谢!

公司:

public class ServiceProvider implements Company { 
    //Variables 


    public ArrayList getRecords(){ 
     return records; 
    } 

    public ServiceProvider(){ 
     money = 10000; 
     yearID = 0; 
     goodYears = 0;badYears = 0; 
     records = new ArrayList(); 
     id++; 
    } 

    public void year() { 
     yearID++; 
     if(!getBankrupt()){ 
      spend(); 
     } 
     writeRecords(); 
    } 

    public void spend() { 
     ... 
    } 

    public void printRecords() { 
     for(int i=0;i<records.size();i++){ 
      String[] tmp = (String[]) records.get(i); 
      for(String a:tmp){ 
       System.out.print(a+" "); 
      } 
      System.out.print("\n"); 


     } 

    } 

    public void writeRecords(){ 
     String[] toWrite = new String[2]; 
     toWrite[0] = String.valueOf(yearID); 
     toWrite[1] = String.valueOf(money); 
     records.add(toWrite); 
    } 

    public void writeRecords(String toWrite){ 
     String temp = "\n"+yearID+" "+toWrite; 
     records.add(temp); 
    } 

    public boolean getBankrupt(){ 
     boolean result = (money < 0) ? true : false; 
     return result; 
    } 


} 

我想从访问:

public class grapher extends JFrame { 
    ArrayList records = s.getRecords(); 

    public grapher(){ 
     super("ServiceProvider"); 
     final XYDataset dataset = getCompanyData(); 
    } 



    private XYDataset getCompanyData(){ 
     XYSeries series; 
     for(int i=0;i<s.getRecords().length;i++){ //S cannot be resolved, it's instantiated in the main class. 

     } 
    } 

} 

主类:

public class start { 

    public static void main(String[] args) { 
     ServiceProvider s = new ServiceProvider(); 
     for(int i=0;i<10;i++){ 
      s.year(); 
     } 
     s.printRecords(); 

    } 

} 

附:别告诉我记录是多么糟糕。我知道。

+0

所有代码中有多少与问题相关?请编辑并删除所有与该问题无关的代码。更好的是,提供例如'ClassA','ClassB'等示例代码 - 参见[SSCCE](http://sscce.org) – Bohemian

回答

0

ServiceProvider的实例作为参数传递给grapher构造函数,然后它可以将它作为参数传递给getCompanyData()

由于实例是在grapher类之外创建的,因此grapher无法让ServiceProvider的实例使用,除非您将该实例交给grapher

顺便说一句,请确保无论您使用grapher中的那个ArrayList做什么,都不会改变它。如果你这样做,你会改变它在ServiceProvider(因为它只是参考相同的底层ArrayList)。这可能不是你想要做的。如果您确实需要更改它,请复制并复制副本。

+0

抱歉。我倾向于啰嗦。 –

0

你的绘图器类应该是如下

public class grapher extends JFrame { 

    public grapher(ServiceProvider s){ 
     super("ServiceProvider"); 
     final XYDataset dataset = getCompanyData(s); 
    } 


    private XYDataset getCompanyData(ServiceProvider s){ 
     XYSeries series; 
     for(int i=0;i<s.getRecords().length;i++){ 
        // Do Process of business logic. 
     } 
    } 

} 
+0

解决方案的问题是什么? –

0

你的绘图器类是试图使用从一开始的类(你正在为它存在于起始类变量s的呼叫)的变量,而无需参考变量。为了图示器访问该实例,你必须在它传递到绘图器类中构造一个paramater:

public grapher(ServiceProvider serviceProvider) { 
    records = serviceProvider.getRecords(); 
} 

在getCompanyData方法,而不是使用是你的类变量记录。

相关问题