简体   繁体   English

在另一个方法中在构造函数中初始化最终变量

[英]Initialize final variable within constructor in another method

I have a problem which isn't really that big, but still gives me some thought as to how Java constructors and methods are used. 我有一个问题并不是那么大,但仍然让我思考如何使用Java构造函数和方法。

I have a constant representing a radius I declare final, and also make it public for everyone to see. 我有一个代表我声明最终的半径的常量,并且还公开供所有人查看。 I don't want my code littered with getRadius() methods when I'm never ever going to change the radius. 当我永远不会改变半径时,我不希望我的代码充斥着getRadius()方法。

I want to initialize the constant within the constructor as I want to apply certain criteria before assigning the radius, certain conditions have to be met. 我想在构造函数中初始化常量,因为我想在分配半径之前应用某些条件,必须满足某些条件。 However, these conditions do take up some space, and I'd like to put them in some other method, to make the constructor cleaner. 但是,这些条件确实占用了一些空间,我想将它们放在其他方法中,以使构造函数更清晰。

The whole thing would initially look like this 整个事情最初看起来像这样

public MyProblematicClass {
   public final int radius;
   public MyProblematicClass(... variables ...) {
      if(... long criteria ...) {
         radius = n;
      }
   }
}

and I'd love it to end up like 而且我喜欢它最终会像

public MyProblematicClass {
       public final int radius;
       public MyProblematicClass(... variables ...) {
          this.setRadiuswithCriteria(criteria);
}

private void setRadiuswithCriteria(criteria crit) {
   if(... crit ...) {
      radius = n;
   }

I understand that I could potentially use the method for other purposes and that's the reason for giving me a 'blank field RADIUS may not have been initialized, so I'd like to know if there is a way to add a method which will only be used in constructors, for cleanliness's sake. 我知道我可能会将该方法用于其他目的,这就是给我一个'空白字段RADIUS的原因可能尚未初始化,所以我想知道是否有办法添加一个只会是用于建造者,为清洁起见。

How about (using small caps for radius, because it is not a constant, as pointed out in the comments): 怎么样(使用小半径的半径,因为它不是常数,如评论中所指出的):

public MyProblematicClass(... variables ...) {
    radius = getRadiusWithCriteria(criteria);
}

private int getRadiusWithCriteria(criteria crit) {
   if(... crit ...) {
      return n;
   } else {
      return 0;
   }
}

You cannot assign to final variable outside constructor. 您不能在构造函数外部分配最终变量。 As you said, method: 如你所说,方法:

setRadiuswithCriteria(criteria crit) {
   if(... crit ...) {
      RADIUS = n;
   }

Can be used outside constructor. 可以在构造函数外部使用。

And you must set final variable to some value in constructor, not just after checking some criteria (always, not sometimes). 并且您必须在构造函数中将final变量设置为某个值,而不仅仅是在检查某些条件后(总是,有时不是)。

However, you might move the code outside the constructor, using the returned value of some function. 但是,您可以使用某个函数的返回值将代码移到构造函数之外。 Example: 例:

class MyClass {
    private final double i;
    public MyClass() {
        i = someCalculation();
    }
    private double someCalculation() {
        return Math.random();
    }
}

How about doing like this? 这样做怎么样?

public MyProblematicClass {
    public final int RADIUS;
    public MyProblematicClass(... variables ...) {
       RADIUS = this.setRadiuswithCriteria(criteria);
}

private int setRadiuswithCriteria(criteria crit) {
if(... crit ...) {
    return n;
}
return 0;
}

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

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