在Java中共享类属性

发布于 2024-12-01 07:41:49 字数 732 浏览 1 评论 0原文

让我们考虑这个例子:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

糖果控 2024-12-08 07:41:49

您需要将属性设为静态

private static int attribute;
        ^^^^^^

声明为静态的成员将在该类的所有实例之间共享。


另外,要使其输出 1 2,您需要更改

s1.incrementAttribue(1);
s2.incrementAttribue(1);

System.out.println(s1.getAttribute());
System.out.println(s2.getAttribute());

s1.incrementAttribue(1);
System.out.println(s1.getAttribute());

s2.incrementAttribue(1);
System.out.println(s2.getAttribute());

ideone.com demo

You need to make the attribute static.

private static int attribute;
        ^^^^^^

Members that are declared static will be shared between all instances of the class.


Also, for it to output 1 2 you need to change

s1.incrementAttribue(1);
s2.incrementAttribue(1);

System.out.println(s1.getAttribute());
System.out.println(s2.getAttribute());

to

s1.incrementAttribue(1);
System.out.println(s1.getAttribute());

s2.incrementAttribue(1);
System.out.println(s2.getAttribute());

ideone.com demonstration

美羊羊 2024-12-08 07:41:49

使属性变量静态

Make the attribute variable static.

各空 2024-12-08 07:41:49

实际上,您的问题标题“在 Java 中共享属性”暗示了解决方案。

在您的示例中,attribute 是一个实例变量,这意味着您的 Shared 类的每个实例(s1s2)获取自己的变量副本,并且它们独立更新。

您需要的是一个类变量,即在类的所有实例之间共享的变量。在 Java 中,这也称为静态变量。将 static 关键字添加到 attribute 变量的声明中,将其从实例变量更改为类变量:

public class Shared {
  private static 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);
    System.out.println(s1.getAttribute());
    s2.incrementAttribute(1);
    System.out.println(s2.getAttribute());
  }
}

- 请注意,您还需要立即打印出该值递增它,否则您将得到输出 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 your Shared class (s1 and s2) 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 the attribute variable to change it from an instance variable to a class variable:

public class Shared {
  private static 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);
    System.out.println(s1.getAttribute());
    s2.incrementAttribute(1);
    System.out.println(s2.getAttribute());
  }
}

—Note that you also need to print out the value immediately after incrementing it, otherwise you get the output 2 2.

默嘫て 2024-12-08 07:41:49

如果您将变量设置为静态,则该变量将由该类的所有实例共享,这正是您在示例中所需要的。对您尝试的 final 关键字进行一些澄清 - final 关键字用于标记只能初始化一次的变量。对于类成员变量,例如您上面定义的变量:

private int attribute;

它必须在声明时或在类构造函数内初始化 - 这就是您无法从方法内为其赋值的原因。

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 the final keyword that you tried - the final 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:

private int attribute;

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.

鼻尖触碰 2024-12-08 07:41:49

由于您的属性是一个实例字段,因此您创建的每个对象都将具有一个属性字段的实例。因此,当您创建 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文