简体   繁体   中英

When Should I use this in a method?

I have this code here and am trying to use :

public class RationalNumber() {
    private int num;
    private int den;

    public RationalNumber(int n, int d) {
        num = n;
        den = d;
    }

    public void multiply(RationalNumber r) {
        /* missing code */
    }

    public int getNum() {
        return num;
    }

    public int getDen() {
        return den;
    }
}

In the public void multiply method, I need The method multiply to multiply the numerator of the RationalNumber by the numerator of r, and the denominator of this RationalNumber by the denominator of r. Which of the following can be used to replace /* missing code */ so that the multiply().

I've narrowed it down to:

num = num * r.num;
den = den * r.den;

or

this.num = this.num * r.num;
this.den = this.den * r.den;

or

num = num * r.getNum();
den = den * r.getDen();

Could someone tell me which one of these (or more than one) will get the method to work as intended?

In short: All three should work, there is only one difference with the last variant: Your class is not final which allows subclasses to change the behavior of getNum and getDen.

Also it's quite uncommon to create classes representing a Number to be mutable. Maybe it's better to change the signature of multiply to be

public RationalNumber multiply(RationalNumber)

that returns a new RationalNumber with the result of the multiplication instead of changing the internal state of the current one. In that case the implementation would be

return new RationalNumber(num * r.num, den * r.num);

(or any other variant you've provided)

The three ways are corret, since:

1. num = num * r.num;
2. den = den * r.den;

Line 1: assigns num (instance variable) * r.num (instance variable of r) result to num. Even num or den having private access, when you deal with the same class inside itself, you you be able to access the private members.

Line 2: assigns den (instance variable) * r.den (instance variable of r) result to den.

this.num = this.num * r.num;
this.den = this.den * r.den;

In this code, you are using the this keyword to explicetely say that the num and den are instance variables of the class. In this case it is not necessary, since you don't have shadowing (when an local variable shadows the name of a instance variable). The logic is the same above.

num = num * r.getNum();
den = den * r.getDen();

You are just using an accessor method (get) to get the values of the private fields. The logic continues the same. Since, as I already said, the current object can access private members of objects of the same class, it will be unecessary.

I would use the first way in this case ;)

All of them are functionally equivalent.

You would need this only if you need to make sure you're referring to the instance variables, instead of local variables or parameters. If your constructor had params named num and den , you would need to write

this.num = num;
this.den = den;

to specify that you want to assign the param num to the instance variable num .

However since there is no ambiguity in the methods, they will all work. You're also able to access the variables r.num and r.den directly, since even though they're private, they're still accessible to the same class , not only the same instance.

I would write it as

num *= r.num;
den *= r.den;

I think the last one will work as intended:

num = num * r.getNum();
den = den * r.getDen();

Actually, there are opposite opinions: use this for class' variable always, to be sure whom this variable belong to. Or another: do use one if there is conflict between local and class variable.

In my work, I use second one: do use this only if I have to. As less words in code, as possible - less problems.

So in you example, you are able to use any variant.

Try this one, I think it's the most correct formally:

this.num = this.num * r.getNum();
this.den = this.den * r.getDen();

Following Lothar's answer, it would be better if the multiply method would return an entire new RationalNumber istance.

I'd even make the method static to the class, since it's just a common operation between RationalNumber s, instead of something that interacts with a single istance of the class modifying it.

In this way, you would call it like this:

RationalNumber mul = RationalNumber.multiply(rn1,rn2);

The method definition would then be:

public static RationalNumber multiply(RationalNumber rn1, RationalNumber rn2){
    return new RationalNumber(rn1.getNum() * rn2.getNum(), rn1.getDen() * rn2.getDen());
}

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