如何在java构造函数中返回变量

时间:2017-02-28 22:02:53

标签: java constructor

我使用构造函数Calcu_weight将值赋给" weight"和" sumweight"。问题是,一旦我调用了Calcu_weight,它就会为权重和加权分配值,但是当我再次调用它时,权重和加权已经分配,​​这会影响第二个构造函数。 我想知道如何使用构造函数" Calcul_weight"很多时候,返回那两个TreeMap变量,但后面的变量不会影响以前的变量。

public class Calcul_weight {

    private TreeMap<int[],Double> weight = new TreeMap<int[],Double>(new SerializableIntegerArrayKeyComparator());
    private TreeMap <Integer,Double> sumweight = new TreeMap <Integer, Double>();

    public Calcu_weight(int originID,int dest, Network network, Dijkstra dijkstra){
        // calculate weight for links. network and dijkstra are another two constructors
    }

    public TreeMap<int[],Double> getWeight(){
        return weight;
    }
    public TreeMap<Integer,Double> getSumweight(){
        return sumweight;
}

1 个答案:

答案 0 :(得分:1)

我猜您需要单身才能在同一weight的不同实例之间保留sumweightCalcul_weight

您还必须重命名该课程Weight或重命名Weight(...)中的Calcul_weight(...)函数才能成为正确的构造函数。)