对变量 [Something] 的赋值无效
The assignment to variable [Something] has no effect
我收到此警告,这是什么意思?
The assignment to variable name has no effect.
我在所有 3 个字段中也遇到了同样的错误。完整代码如下:
public class Module {
private String name;
private int bind;
private Category category;
public Module(String name,int bind,Category category) {
}{
this.name = name; // The assignment to variable name has no effect
this.bind = bind;
this.category = category;
}
这是你的问题,额外的括号:
public Module(String name,int bind,Category category) {
}{ // <<<
this.name = name;
this.bind = bind;
this.category = category;
}
应该是:
public Module(String name,int bind,Category category)
{
this.name = name;
this.bind = bind;
this.category = category;
}
public Module(String name,int bind,Category category) {
}{
有一对额外的大括号,所以您的代码实际上不在构造函数中。
您的代码将运行before构造函数作为"instance initialization block",因此,这些命名参考class 的实例,而不是构造函数参数。
只是为了完成给出的答案。
您的代码完全有效。实际上,您创建了一个不分配任何变量的构造函数:
public Module(String name,int bind,Category category) {
}
在下面你创建了一个被每个构造函数调用的 initializer code block :
{
this.name = name;
this.bind = bind;
this.category = category;
}
此块有效地将每个局部变量分配给自身,这会导致编译器对此发出警告。
我收到此警告,这是什么意思?
The assignment to variable name has no effect.
我在所有 3 个字段中也遇到了同样的错误。完整代码如下:
public class Module {
private String name;
private int bind;
private Category category;
public Module(String name,int bind,Category category) {
}{
this.name = name; // The assignment to variable name has no effect
this.bind = bind;
this.category = category;
}
这是你的问题,额外的括号:
public Module(String name,int bind,Category category) {
}{ // <<<
this.name = name;
this.bind = bind;
this.category = category;
}
应该是:
public Module(String name,int bind,Category category)
{
this.name = name;
this.bind = bind;
this.category = category;
}
public Module(String name,int bind,Category category) {
}{
有一对额外的大括号,所以您的代码实际上不在构造函数中。
您的代码将运行before构造函数作为"instance initialization block",因此,这些命名参考class 的实例,而不是构造函数参数。
只是为了完成给出的答案。
您的代码完全有效。实际上,您创建了一个不分配任何变量的构造函数:
public Module(String name,int bind,Category category) {
}
在下面你创建了一个被每个构造函数调用的 initializer code block :
{
this.name = name;
this.bind = bind;
this.category = category;
}
此块有效地将每个局部变量分配给自身,这会导致编译器对此发出警告。