2个实例变量不断相互更新

时间:2017-03-02 02:49:58

标签: java

我遇到一个小问题,我的实例变量有点像类变量,至少就是我的想法。

public class Lab10 {
    public static void main(String[] args) {
        CannonSolution MaxRange = MAXRMHC(10);
        MaxRange.println();
    }

    public static CannonSolution MAXRMHC(int iter){
        double range =0;
        double newrange;
        CannonSolution sol = new CannonSolution(1,1);
        CannonSolution NewSol = new CannonSolution(sol.SendCanang(),sol.SendCanvel());
        range = Cannon.GetMaxRange(sol.SendCanang(),sol.SendCanvel());
        for(int i =0;i<iter;i++){
            NewSol.smallChange();
            newrange = Cannon.GetMaxRange(NewSol.SendCanang(), NewSol.SendCanvel());
            if(range <= newrange){
                System.out.println(range+" is smaller than "+newrange);
                NewSol.println();
                range = newrange;
                sol = NewSol;
            }else if (range > newrange){
                NewSol.println();
                sol.println();
            }else{
                System.out.println("I don't work");
            }
        }
        System.out.println();
        System.out.println(range);
        return (sol);
    }
}


public class CannonSolution {


    private double canang;
    private double canvel;

    public CannonSolution(double ang, double vel){

        if(ang <25.0 || ang >55.0){
            canang = Randomang();
        }else{
            canang = ang;
        }
        if(vel <1500.0 || vel > 1650.0){
            canvel = Randomvel();
        }else{
            canvel = vel;
        }
    }
    public double SendCanang(){
        return (canang);
    }
    public double SendCanvel(){
        return (canvel);
    }

    private static double Randomang(){
        double ang = RandomNumber.RandomNum(25,55);
        return ang;
    }
    private static double Randomvel(){
        double vel = RandomNumber.RandomNum(1500,1650);
        return vel;
    }
    private void smallChangeVel(){
        double vel = canvel;
        double number = (double) RandomNumber.UI(1,30);
        double updown = RandomNumber.UI(1,2);
        if(updown ==1 ){
            vel = vel + number;
        }else{
            vel = vel - number;
        }
        if(vel <1500){
            vel =1500.0;
        }else if (vel > 1650){
            vel = 1650.0;
        }
        canvel = vel;
    }
    private void smallChangeAng(){
        double ang = canang;
        double number = RandomNumber.UI(1,3);
        double updown = RandomNumber.UI(1,2);
        if(updown ==1 ){
            ang = ang + number;
        }else{
            ang = ang - number;
        }
        if(ang <25){
            ang =25.0;
        }else if (ang > 55){
            ang = 55.0;
        }
        canang= ang;
    }
    public void smallChange(){
        double updown = RandomNumber.UI(1,2);
        if(updown == 1){
            smallChangeAng();
        }else{
            smallChangeVel();
        }
    }
        public void print()
        {
            System.out.println(canang);
            System.out.print(canvel);
        }

        public void println()
        {
            print();
            System.out.println();
        }
    }

http://pastebin.com/zJWPyz7Q    http://pastebin.com/EYugr96p

问题是当使用smallchange方法更改Newsol时,它会更改newsol和sol。我已经坚持了一段时间。

很抱歉,我不太确定如何格式化这个新的问题,在stackoverflow中询问。

1 个答案:

答案 0 :(得分:0)

行后

sol = Newsol;

您已放弃sol引用的旧对象,现在变量solNewsol都标识了同一个对象。这会导致您看到的行为。