简体   繁体   中英

Sending arrays from actionPerformed() to another class

I have a calculation in an actionPerformed() method in class A . The results are two arrays containing doubles C[] and D[] . How can I send it to another class B ?

There any number of ways you could achieve this.

The important parts are:

  • Have a reference to B
  • Have some kind of means for B to receive the values you want to send. I'd recommend a setter method of some kind

You could also use a common model, which is essentially the same, but exposes less about B to A making it more difficult for A to do naughty things to B it probably shouldn't ;)

You have tons of ways. The correct one for you depends on the structure of object model you use.

However, there are two basic concept you have to understand:

1. What is the "intersection point".

In order for A 's instance (call it a1 ) to be able to communicate with B 's instance ( b1 ), a1 must have a way to "put his hands" on a reference to b1 . A few example ways to do it are:

  • Each A get's a reference to B 's instance as an argument to it's constructor, or another 'set' method, and then stores it as a field made for that purpose.
  • a1 and b1 share a common instance of class C . c can be the 'parent' of both a1 and b1 (ie contain both of them), or some 'manager' component that manage a certain process in your program.
  • The static \\ singleton way. B class stores a static instance of itself, which allows reference to it from everywhere in the program.

2. What is the the desired communication interface

The interface one class exposes to others should be well designed in order to achieve many important concepts, eg: readability of code, security and 'hiding', reliability etc. This also depends on if both A and B are stored in the same package or are internal classes of each other or are even inherited from each other.

A few standard ways to communicate are:

  • Direct writing access: a field in B is public ly exposed and allows direct writing into it. this is mostly a bad behavior (a rule of thumb is that all fields should be at least protected if not private ).
  • set methods: B has a set method that receives the calculated data, process it and stores it in it's fields (or pass it on).
  • Listening - A stores the calculated data in itself, and lets all of it's registered "listeners" know that a new data is available by calling an appropriate method in them. The listeners have read access to the relevant fields, or the data is passed as an argument, and then the listener ( b1 ) decides what to do with the data.

Create an instance of class B and pass the result of the calculation to that instance.

This is a very basic problem and is easy to solve once you've had some experience. You might find the wiki on Object-oriented programming useful. Its powerful stuff once you understand it.

Here's some examples for you...

public class ClassB{
    double multiplier;
    public ClassB(){
        //this will be called when you create an instance of class B
        //with no arguments. Also called the default constructor, or empty-arg
        //constructor.
        multiplier = 2;
    }
    public void setMultiplier(double value){
        multiplier = value;
    }
    public void calculate(double[] c, double[] d){
        //do nifty stuff, like multiply every value in c and d!
        for(int i=0;i<c.length;i++){
            c[i] *= multiplier;
            d[i] *= multiplier;
        }
    }
}

// and in your class having the action performed method...
//create an instance of class b. This will call the empty-arg constructor.
ClassB classB1 = new ClassB();
ClassB classB2 = new ClassB();
public void actionPerformed(ActionEvent e){
    double[] c1 = new double[10];
    double[] d1 = new double[10];
    double[] c2 = new double[10];
    double[] d2 = new double[10];
    //call classB1 calculate method. This will multiply c1 and d1 by 2,
    //as classb1's multiplier is 2.
    classB1.calculate(c1, d1);
    classB2.setMultiplier(4);
    //call classB2 calculate method. This will multiply c2 and d2 by 4,
    //as classb2's multipler has been set to 4.
    classB2.calculate(c2, d2);
}

This is just a simple example of what object oriented programming can do for you. There's a ton more to learn than this, but as you're obviously just beginning I won't overwhelm you.

Try making both arrays C & D static. Making these static will allow other classes access to it. Otherwise, you should pass it through a parameter to the constructor.

Parameter Method:

public class A implements ActionListener{

Array C;
Array D;

Bclass B = new Bclass(C, D);

    public void actionPerformed(){
        //Do stuff to Arrays

    }

}

public class Bclass{

    Bclass(Array C, Array D){
        //This is Constructor...
    }

}

Static Method:

public class A implements ActionListener{

public static Array C;
public static Array D;

    public void actionPerformed(){
        //Do stuff to Arrays

    }

}

public class Bclass{

    Bclass(){
        //This is Constructor...
        A.C().getIndexOf("C-stuff");
        A.D().getIndexOf("D-stuff");
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM