开发者

Recursive Set method in Java - Default variable a la Perl?

The only other language I've any experience with is Perl & this is my first crack at OO programming. I feel like I'm approaching this all wrong. One of the problems is probably me trying to code OO Java like I coded non-OO Perl. Can anyone suggest a way to gracefully accomplish what I'm trying to accomplish in the code snippet below?

Note: The ??? in my code is where I would want to use the default object.

public class Var {
    private double var1;

    public Var (double PassedVar1) {  //method to create new object
        var1 = ???.SetVar1(PassedVar1);
    }

    public void SetVar1 (double PassedVar) {
        if (PassedVar > 0) {  //make sure we're assigning a positive value
            var1 = PassedVar;
        }
        else {  //force user to input a new value
            System.out.print( "\nFailed to set var.  " +
            "Please enter a number greater than zero: ");
            Sca开发者_JAVA百科nner scan = new Scanner (System.in);
            PassedVar = scan.nextDouble();
            var1 = ???.SetVar1(PassedVar);//recurse call to assure positive
        }
    }
}


The word you're looking for is this (i.e. this.SetVar1(...)), however in Java that's not necessary. You can just use SetVar1(...) in any non-static member of Var of any subclasses of Var.

No doubt others will offer advice on a better way to accomplish your task in Java, so I won't answer that part of the question.


Your code would become this, after a little refactoring and fixing,

public class Var {
    private double var1;

    public Var (double passedVar1) {  //method to create new object
        setVar1(passedVar1); // You just need to invoke setVar1() by passing the argument
    }

    public void setVar1 (double passedVar1) {
        if (passedVar1 > 0) {  //make sure we're assigning a positive value
            var1 = passedVar1;
        } else {  //force user to input a new value
            System.out.print( "\nFailed to set var.  " +
                                   "Please enter a number greater than zero: ");
            Scanner scan = new Scanner (System.in);
            passedVar1 = scan.nextDouble();
            setVar1(passedVar1); //recurse call to assure positive
        }
    }
}

Okay because of critics, I dare to change it drastically.

public class Var {

    private double var1;

    private Var(double var1) { 
        this.var1 = var1;
    } 

    public static final Var getVar(double var1) { 
        return (var1 > 0) ? new Var(var1) : null;
    }    
}

In this way the calling code should check for null and do whatever it likes to on encountering null. Anyways, below is the code snippet that is making sure that the input double is valid and then instantiating Var.

         ...
         double passedVar1 = 0D;
         do {
            System.out.print( "\nFailed to set var.  " +
                                   "Please enter a number greater than zero: ");
            Scanner scan = new Scanner (System.in);
            passedVar1 = scan.nextDouble();
         } while (passedVar1 < 0)

         Var var = Var.getVar(passedVar1);
         ...

Cheers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜