简体   繁体   English

什么时候应该在方法中使用它?

[英]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. 在public void乘积法中,我需要The方法乘以将RationalNumber的分子乘以r的分子,并且将该RationalNumber的分母乘以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. 简而言之:这三个都应该起作用,与最后一个变量只有一个区别:您的类不是最终的,它允许子类更改getNum和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 也许最好将multiply更改为

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. 它返回一个新的RationalNumber并带有相乘的结果,而不是更改当前乘积的内部状态。 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. 第1行:将num(实例变量)* r.num(r的实例变量)结果分配给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. 即使是numden都有私有访问权限,当您处理内部的同一类时,您也可以访问私有成员。

Line 2: assigns den (instance variable) * r.den (instance variable of r) result to den. 第2行:将den(实例变量)* r.den(r的实例变量)的结果分配给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. 在此代码中,您使用this关键字明确表示num和den是该类的实例变量。 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. 您需要this ,如果你需要确保你指的不是局部变量或参数的实例变量,只。 If your constructor had params named num and den , you would need to write 如果您的构造函数具有名为numden参数,则需要编写

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

to specify that you want to assign the param num to the instance variable num . 指定您要将参数num分配给实例变量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. 您还可以直接访问变量r.numr.den ,因为即使它们是私有的,它们仍可被同一访问,而不仅限于同一实例。

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. 事实上,也有相反的观点:使用this总是类的变量,以确保谁该变量属于。 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. 在我的工作,我用第二个:不使用this唯一的,如果我不得不这样做。 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. 按照Lothar的回答,最好multiply方法返回一个全新的RationalNumber距离。

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. 我什至使该方法对类是静态的,因为它只是RationalNumber之间的常用操作,而不是与修改类的单个方法交互的操作。

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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM