在Java中共享类属性
让我们考虑这个例子:
public class Shared {
private int attribute;
public Shared() {}
public void incrementAttribute(int i) {
attribute += i;
}
public int getAttribute() {
return attribute;
}
public static void main(String[] args) {
Shared s1 = new Shared();
Shared s2 = new Shared();
s1.incrementAttribute(1);
s2.incrementAttribute(1);
s1.getAttribute();
s2.getAttribute();
}
}
当调用 getAttribute()
时,如何更改此类以在输出中包含 1
2
而不是 1< /code>
1
像全局变量一样,我尝试了 final
关键字,但无法使用方法设置某些内容。
Let's consider this example :
public class Shared {
private int attribute;
public Shared() {}
public void incrementAttribute(int i) {
attribute += i;
}
public int getAttribute() {
return attribute;
}
public static void main(String[] args) {
Shared s1 = new Shared();
Shared s2 = new Shared();
s1.incrementAttribute(1);
s2.incrementAttribute(1);
s1.getAttribute();
s2.getAttribute();
}
}
How can I change this class to have 1
2
in output when calling getAttribute()
and not 1
1
Something like a global variable, I tried the final
keyword but I can't set something using a Method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要将属性设为
静态
。声明为静态的成员将在该类的所有实例之间共享。
另外,要使其输出
1 2
,您需要更改为
ideone.com demo
You need to make the attribute
static
.Members that are declared
static
will be shared between all instances of the class.Also, for it to output
1 2
you need to changeto
ideone.com demonstration
使属性变量
静态
。Make the attribute variable
static
.实际上,您的问题标题“在 Java 中共享类属性”暗示了解决方案。
在您的示例中,
attribute
是一个实例变量,这意味着您的Shared
类的每个实例(s1
和s2
)获取自己的变量副本,并且它们独立更新。您需要的是一个类变量,即在类的所有实例之间共享的变量。在 Java 中,这也称为静态变量。将
static
关键字添加到attribute
变量的声明中,将其从实例变量更改为类变量:- 请注意,您还需要立即打印出该值递增它,否则您将得到输出 2 2。
You actually hinted at the solution with your question title "Sharing class attributes in Java".
In your example
attribute
is an instance variable which means that each instance of yourShared
class (s1
ands2
) gets it own copy of the variable and they are updated independently.What you need is a class variable i.e. something that is shared across all instances of a class. In Java this is also called a static variable. Add the
static
keyword to your declaration of theattribute
variable to change it from an instance variable to a class variable:—Note that you also need to print out the value immediately after incrementing it, otherwise you get the output 2 2.
如果您将变量设置为静态,则该变量将由该类的所有实例共享,这正是您在示例中所需要的。对您尝试的
final
关键字进行一些澄清 -final
关键字用于标记只能初始化一次的变量。对于类成员变量,例如您上面定义的变量:它必须在声明时或在类构造函数内初始化 - 这就是您无法从方法内为其赋值的原因。
If you make the variable
static
it is shared by all instances of the class, which is what you require in your example. To give some clarification with regards to thefinal
keyword that you tried - thefinal
keyword is used to mark a variable that can only be initialized once. For a class member variable, such as the one you defined above:it must initialised either at declaration or within the class constructor - this is why you were unable to assign a value to it from within a method.
由于您的属性是一个实例字段,因此您创建的每个对象都将具有一个属性字段的实例。因此,当您创建 2 个对象 s1 和 s2 时,它们都有单独的属性字段。
如果你想让类的所有实例共享值,你需要有类级别的字段,为此你必须将属性变量声明为静态,并且它的值将取决于所有实例对increment方法的调用。
As your attribute is an instance field, each object you create will have an instance of attribute field with them. So when you are creating 2 objects s1 and s2 they both have individual attribute field.
If you want to have value shared by all the instances of the class, you need to have class level fields, for which you have to declare the attribute variable as static, and the value of it will depend on all the instance's call to increment method.