Java - 使用来自另一个类

时间:2017-05-03 13:45:01

标签: java

我在java中有2个类。我想在类Population中使用Graph“nr_noduri”类中的变量。我怎样才能做到这一点?问题是,当我创建对象时,我在主要选择“nr_noduri”。例如,图g =新图(5)。

package tema_top_v3;

public class Graph {

    public int nr_noduri ;
    int [][]a;

    Graph(int v){
    this.nr_noduri = v;
    a = new int [v][v];
    }

    void link(int x, int y){
        a[x][y] = 1;  
    }

}

public class Population {

    int X = 50;

    public void generate_population(int N,int nr_culori){
        N = X;

        String [][]X = new String [N][nr_noduri];
        String colors1[] = {"red", "blue", "green"};   
        String colors2[] = {"red", "blue", "green", "yellow"};  

        if(nr_culori==3){
        for(int i = 0; i<N;i++){
           for(int j = 0; j<nr_noduri; j++){
               X[i][j] =  colors1[new Random().nextInt(colors1.length)];
           }         
        } 
        }

        if(nr_culori==4){
            for(int i = 0; i<N;i++){
               for(int j = 0; j<nr_noduri; j++){
                   X[i][j] =  colors2[new Random().nextInt(colors2.length)];
               }         
            } 
            }

    }
}

0 个答案:

没有答案