当您在 Java 中调用 new 时,它会创建构造函数还是类的副本?

发布于 2024-10-15 21:41:21 字数 959 浏览 4 评论 0原文

我目前正在做一项 Java 作业,出于某种原因,它可以工作 - 但在我看来,它不应该!我拥有的是一个 main 方法,通过调用

panel.add(new ButtonApp());

ButtonApp 创建三个新按钮,这是我的 ButtonApp 类中定义的构造函数。

在课程开始时,我声明了一个变量,如下所示:

public int clicks = 0;

该变量的目的是跟踪用户单击其特定 ButtonApp 的次数。每次点击后都会调用 clicks++。

现在,在我看来,每次单击按钮时,clicks 变量都应该增加,因此,如果单击左侧按钮,它也应该增加中间和右侧按钮。

所以本质上我所拥有的,简短的伪代码是:

public class ButtonApp() {
    public int clicks =0;

    public static void main(String[] args) {
        //create JPanel/Frame etc etc
        panel.add(new ButtonApp());
        panel.add(new ButtonApp());
        panel.add(new ButtonApp());
    }

    public ButtonApp(){
        //creates a new button
        this.actionListener(this);
    }

    public void update(){
        clicks++;
    }

    public void actionPerformed (ActionEvent event){
        update();
    }
}

我不是总是在这里增加一个 clicks 变量吗?

I am currently working on a Java assignment, and for some reason, it works - but to my mind, it shouldn't ! What I have is a main method, creating three new buttons with the call

panel.add(new ButtonApp());

ButtonApp is the constructor defined in my ButtonApp class.

At the beginning of my class, I have a variable declared as follows :

public int clicks = 0;

The purpose of this variable is to keep track of the amount of times the user clicks on their specific ButtonApp. clicks++ is called after every click.

Now, in my mind, every time you click the button, the clicks variable shoudl get incremented, and so, if you click the left button, it should increment the middle and right buttons also.

So essentially what I have, in quick short psuedocode, is :

public class ButtonApp() {
    public int clicks =0;

    public static void main(String[] args) {
        //create JPanel/Frame etc etc
        panel.add(new ButtonApp());
        panel.add(new ButtonApp());
        panel.add(new ButtonApp());
    }

    public ButtonApp(){
        //creates a new button
        this.actionListener(this);
    }

    public void update(){
        clicks++;
    }

    public void actionPerformed (ActionEvent event){
        update();
    }
}

Am I not always incrementing one clicks variable here ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

我只土不豪 2024-10-22 21:41:21

我不是总是在这里增加一次点击变量吗?

不,您正在处理实例变量。使用 new 创建的每个对象都有该变量的一个副本

如果您将代码更改为:

public static int clicks =0;

您确实只有一个可以多次更改的变量(静态字段属于类,实例字段属于实例)

参考:

(全部来自Java教程>学习Java 语言 > 类和对象 部分)

Am I not always incrementing one clicks variable here ?

No, you are dealing with instance variables. There is one copy of this variable per object created with new

If you changed the code to this:

public static int clicks =0;

You would indeed only have one variable that would be changed multiple times (static fields belong to the class, instance fields belong to the instance)

Reference:

(All from the Java Tutorial > Learning the Java Language > Classes and Objects section)

帅冕 2024-10-22 21:41:21

new() 创建类的 DATA 实例——即任何成员变量。代码不重复。在您的情况下,每个类都有自己的“clicks”成员变量。

如果您将 clicks 声明为 static,那么它将是一个“类变量”,属于类本身而不是其实例,然后就只有一个单份。但是,您没有将其声明为静态,因此每个实例(即由 new() 创建的每个“事物”)都会获得自己的副本。

new() creates an instance of the class' DATA -- i.e. any member variables. Code is not duplicated. In your case, each class has its own 'clicks' member variable.

If you had declared clicks to be static, then it would be a "class variable", belonging to the class itself and not its instances, and then there would be only a single copy. However, you did not declare it static, so each instance (i.e. each "thing" that was created by new()) gets its own copy.

魔法唧唧 2024-10-22 21:41:21

您创建的 ButtonApp 的 3 个不同对象将有 3 个点击变量

panel.add(new ButtonApp());
panel.add(new ButtonApp());
panel.add(new ButtonApp());

如果您想计算总点击次数,则

,忽略哪个按钮点击,只需使 click 变量 static目前每个变量对象有一个不同的 click 变量,但如果将其设为静态,则每个加载的类都会相同

另请参见

There will be 3 click variable for 3 different object of ButtonApp you have created by

panel.add(new ButtonApp());
panel.add(new ButtonApp());
panel.add(new ButtonApp());

if you want to count total clicks , ignoring which button clicks just make click variable static

Currently with each object a different click variable is there, but if you make it static it would be same per class loaded

Also See

很酷不放纵 2024-10-22 21:41:21

每个 ButtonApp 都有自己的 clicks 变量。如果您有三个按钮,则有三次点击,并且给定的点击仅在按下其按钮时才会递增。

如果您想要一个click 变量,您可以将其设为类变量,而不是实例变量。这是通过 static 关键字完成的:

private static int clicks = 0;

不过,一般来说,最好避免使用静态成员。一种更复杂但更灵活的方法是在要链接的所有按钮之间共享一个计数器对象。您可以创建自己的,但是 AtomicInteger 是一个自然的选择,如果您要从 UI 线程外部访问此状态,它可以提供线程安全性。

class ButtonApp {

  private final AtomicInteger clicks;

  ButtonApp(AtomicInteger clicks) { this.clicks = clicks; }

  void update() {
    clicks.incrementAndGet();
  }

  ...

}

Every ButtonApp has its own clicks variable. If you have three buttons, you have three clicks, and a given click is only incremented when its button is pressed.

If you want one click variable, you could make it a class variable, instead of an instance variable. This is done with the static keyword:

private static int clicks = 0;

In general, though, it's better to avoid static members. A more complex, but flexible, approach would be to share a counter object among all buttons that you want to link. You could create your own, but AtomicInteger is a natural fit, and provides thread safety if you will be accessing this state from outside the UI thread.

class ButtonApp {

  private final AtomicInteger clicks;

  ButtonApp(AtomicInteger clicks) { this.clicks = clicks; }

  void update() {
    clicks.incrementAndGet();
  }

  ...

}
兮颜 2024-10-22 21:41:21

您创建 ButtonApp 类的三个实例,因此分别递增三个属性。

您可能会感到困惑,因为您在同一个类中嵌入了 main 方法,并且您认为您正在增加与 main() 相关的实例的 clicks 属性

请记住,main静态,这意味着您的类没有与main 相关的实例。尝试从 main 访问 clicks,您会看到错误。

You create three instances of your ButtonApp class, so you increment the three attributes separately.

You might be confused because you embedded the main method in the same class, and you think that you are incrementing the clicks attribute of the instance related to main().

Keep in mind that main is static, and that means that there is no instance of your class related to main. Try to access clicks from main and you'll see an error.

醉城メ夜风 2024-10-22 21:41:21
panel.add(new ButtonApp());

将通过调用零参数/默认构造函数来创建 ButtonApp 类的新实例

对于每个新实例,您将拥有一组新成员,包括点击,因此每次都会为零。您的代码不会重复,但您将拥有此类的不同实例,并具有自己的数据集。

panel.add(new ButtonApp());

will create a new instance of your ButtonApp class, by calling your zero-arg/default constructor.

For each new instance, you'll have a new set of members, inclding clicks, and consequently that will be zero each time. Your code is not duplicated, but you will have different instances of this class, with their own set of data.

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