将构造函数方法拆分为多个部分 - 最终值的问题
我想将类的构造函数分成几个部分。但我有一个问题...
是否可以在构造函数内部调用的方法中初始化最终值?必须直接在构造函数中初始化吗?
这...
import java.util.Scanner;
public final class A
{
private final int L;
private final int D;
private final int N;
public A()
{
Scanner scanner = new Scanner(System.in);
this.getFirstLine(scanner);
/* the rest of the constructor method */
}
private void getFirstLine(Scanner scanner)
{
this.L = scanner.nextInt();
this.D = scanner.nextInt();
this.N = scanner.nextInt();
}
}
给了我类似于无法分配最终字段 AL
的错误。
那么它被视为一个作业吗?是的?
有没有一种方法可以拆分构造函数来实现我想要的?
提前致谢。
I wanted to split the constructor of my class into parts. But I have a problem...
Is it possible to initialize a final value in a method called inside constructor? It has to be initialized directly in constructor?
This...
import java.util.Scanner;
public final class A
{
private final int L;
private final int D;
private final int N;
public A()
{
Scanner scanner = new Scanner(System.in);
this.getFirstLine(scanner);
/* the rest of the constructor method */
}
private void getFirstLine(Scanner scanner)
{
this.L = scanner.nextInt();
this.D = scanner.nextInt();
this.N = scanner.nextInt();
}
}
gives me errors similar to The final field A.L cannot be assigned
.
So it is treated as an assignment? Yeah?
Is there a way of splitting constructor in order to achieve what I wanted?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这个怎么样。
How about this.
您不能根据 Java 语言规范< /a>:
您可以在构造函数中执行此操作,因为编译器可以保证每个实例仅调用构造函数一次。下面的代码可以编译。我刚刚验证了它:
您不能在非构造函数方法中分配最终变量,因为不能保证每个实例仅调用一次。
You cannot do this according to the Java Language Spec:
You can do it in the constructor because the compiler can guarantee that the constructor will only be called once per instance. The code below compiles. I just verified it:
You cannot assign the final variables in your non-constructor method because it cannot be guaranteed to be called only once per instance.
不,这是不可能的。
它必须在每个构造函数的末尾进行初始化。初始化可以在声明或实例初始化程序中完成......作为所有构造函数的一部分执行。
是的!
问题是该方法可以在任何时候调用:
JLS 不要求编译器进行复杂的分析以确保赋值在构造期间(而不是构造之后)发生一次且仅发生一次,而是简单地禁止在该上下文中进行赋值。
您可以在单独的方法中计算值,但
final
的实际初始化必须在构造函数、声明初始值设定项或初始值设定项块中执行。(如果框架中有子类,则可能会使用其他技巧。)
No it is not possible.
It has to be initialized by the end of each and every constructor. The initialization can be done in the declaration or an instance initializer ... that gets executed as part of all constructors.
Yeah!
The problem is that the method could be called at any point:
Rather than requiring the compiler to do a complicated analysis to ensure the assignment happens once and only once during construction (and not after construction), the JLS simply forbids the assignment in that context.
You can calculate the values in a separate method, but the actual initialization of the
final
must be performed in the constructors, the declaration initializer or an initializer block.(If there were subclasses in the frame, there are other tricks that could possibly be used.)
最终成员只能在实例初始化期间设置。因此,您可以在声明时、在初始化块或构造函数中初始化 Final 字段。为什么不将
getFirstLine
设为私有构造函数:final members can only be set during initialisation of the instance. So you can initialize final fields when you declare it, in an initialization block or in a constructor. Why don't you make
getFirstLine
a private constructor:好的,这就是我受到答案启发后所做的:
我创建了负责读取输入的私有静态类。
现在应该更符合逻辑地将其划分为单独的部分。
OK, that's what I've done after being inspired by answers:
I've made private static class that is responsible for reading input.
Now it should be more logically divided into separate parts.