【问题标题】:Accessing Instance Variables from Another Class从另一个类访问实例变量
【发布时间】:2012-05-21 02:04:39
【问题描述】:

如果您想直接解决问题,请跳过本段。作为实践,我正在尝试编写一个模拟经济的 Java 程序,并为此编写了一个公司类。这个想法是让他们中的一打,将他们的收入包装成一个正态变量函数,这就是经济。

我编写了一个单独的类来使用 JFreeChart 绘制公司的输出。但是,我无法从绘图类访问我写每年的金额的 ArrayList。我知道最好的方法可能是使用吸气剂,但它似乎不起作用,所以如果这是你的建议,你能提供一个例子吗?谢谢!

公司:

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();

    }

}

附:不要告诉我记录是什么乱七八糟的。我知道。

【问题讨论】:

  • 所有代码中有多少(如果有)与问题相关?请编辑并删除与问题不直接相关的所有代码。更好的是,提供ClassAClassB 等示例代码 - 请参阅SSCCE

标签: java permissions instance


【解决方案1】:

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

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

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

【讨论】:

  • 对不起。我倾向于啰嗦。
【解决方案2】:

您的绘图员类正在尝试使用起始类中的变量(您正在调用起始类中存在的变量 s),而没有对该变量的引用。为了让 grapher 访问该实例,您必须将其作为构造函数中的参数传递给 grapher 类:

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

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

【讨论】:

    【解决方案3】:

    你的grapher类应该如下

    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.   
            }
        }
    
    }
    

    【讨论】:

    • 解决方案有什么问题?
    猜你喜欢
    • 1970-01-01
    • 2013-10-28
    • 1970-01-01
    • 2020-04-14
    • 2011-04-28
    • 2012-01-23
    • 2011-07-26
    • 1970-01-01
    相关资源
    最近更新 更多