当您在 Java 中调用 new 时,它会创建构造函数还是类的副本?
我目前正在做一项 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不,您正在处理实例变量。使用
new
创建的每个对象都有该变量的一个副本如果您将代码更改为:
您确实只有一个可以多次更改的变量(静态字段属于类,实例字段属于实例)
参考:
变量
实例和类成员
(全部来自Java教程>学习Java 语言 > 类和对象 部分)
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:
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:
Variables
Instance and Class Members
(All from the Java Tutorial > Learning the Java Language > Classes and Objects section)
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 bestatic
, 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.您创建的 ButtonApp 的 3 个不同对象将有 3 个点击变量
如果您想计算总点击次数,则
,忽略哪个按钮点击,只需使
click
变量static
目前每个变量对象有一个不同的click
变量,但如果将其设为静态,则每个加载的类都会相同另请参见
static
字段There will be 3 click variable for 3 different object of ButtonApp you have created by
if you want to count total clicks , ignoring which button clicks just make
click
variablestatic
Currently with each object a different
click
variable is there, but if you make it static it would be same per class loadedAlso See
static
Fields from jls每个
ButtonApp
都有自己的clicks
变量。如果您有三个按钮,则有三次点击
,并且给定的点击
仅在按下其按钮时才会递增。如果您想要一个
click
变量,您可以将其设为类变量,而不是实例变量。这是通过static
关键字完成的:不过,一般来说,最好避免使用静态成员。一种更复杂但更灵活的方法是在要链接的所有按钮之间共享一个计数器对象。您可以创建自己的,但是
AtomicInteger
是一个自然的选择,如果您要从 UI 线程外部访问此状态,它可以提供线程安全性。Every
ButtonApp
has its ownclicks
variable. If you have three buttons, you have threeclicks
, and a givenclick
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 thestatic
keyword: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.您创建
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 theclicks
attribute of the instance related tomain()
.Keep in mind that
main
isstatic
, and that means that there is no instance of your class related tomain
. Try to accessclicks
frommain
and you'll see an error.将通过调用零参数/默认构造函数来创建
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.