简体   繁体   English

以这两种方式创建对象有什么区别?

[英]What is the difference between creating an object in these two way?

i wonder what the difference is between these: 我想知道这些之间的区别是什么:

1-) 1-)

JFrame frame = new JFrame();
JLabel label = new JLabel("example");
frame.add(label);

2-) 2-)

JFrame frame = new JFrame();
frame.add(new Label("example"));

also, we can use the syntax like this: 另外,我们可以使用如下语法:

1-) 1-)

new Timer(10, new ActionListener() {...}).start();

but why cannot we use it like this: 但为什么我们不能像这样使用它:

2-) 2-)

Timer timer = new Timer(10, new ActionListener() {...}).start(); // we cannot use it this way it has to be like:

//Timer timer = new Timer(10, new ActionListener() {...});
//timer.start();

the difference between these two are like this, 这两者之间的区别是这样的,

if you don't require the reference variable to object while sending that as an argument to a method you can directly generate a object while calling the method. 如果在将参数变量作为参数发送给方法时不需要引用变量,则可以在调用方法时直接生成对象。

like this frame.add(new Label("example")); 喜欢这个frame.add(new Label("example"));

but if you do require the object you would be passing as argument, it would be better to have it's reference variable so you can do something with it latter in code, 但是如果你确实要求你将作为参数传递的对象,那么最好有它的引用变量,这样你就可以在代码中用它做一些事情,

let's say you want to change some instance variable or get he status of Object after the method completed it's task. 假设您想要在方法完成任务后更改某个实例变量或获取Object状态。 In such cases you would require the reference variable to the Object 在这种情况下,您需要引用变量到Object

and yes answer to your other question is given by Ramanlfc Ramanlfc给出了你的另一个问题的回答

which says new Timer(10, new ActionListener() {...}).start(); 其中说new Timer(10, new ActionListener() {...}).start();

this can be done as the method return type is ignored here, this task will be done as the statement issued, 这可以在这里忽略方法返回类型,这个任务将在语句发布时完成,

but

Timer timer = new Timer(10, new ActionListener() {...}).start(); 

this is not possible as start() does not return Timer which is being assigned to the Timer reference variable 这是不可能的,因为start()不返回Timer ,其被分配给Timer参考变量

Timer timer = new Timer(10, new ActionListener() {...}).start();

assignments are right to left ,so what your expression evaluates to should be assignable to the operand on the left of = . 赋值是从右到左,所以你的表达式求值应该可以赋值给=左边的操作数。 start() will not return Timer in this case ,so it fails. 在这种情况下, start()不会返回Timer ,因此失败。

In the 2 first examples you're creating a JFrame and assigning it to a JFrame variable. 在前面的两个示例中,您将创建一个JFrame并将其分配给JFrame变量。 Same with the label. 与标签相同。

Timer timer = new Timer(10, new ActionListener() {...}).start();

Will not work because the return type of 因为返回类型不会起作用

start();

is void. 是无效的。

new Timer(10, new ActionListener() {...}).start();

Works because you never try to assign the void return value to a Timer. 之所以有效,是因为您从未尝试将void返回值分配给Timer。

The other Answers are correct. 其他答案是正确的。

Another way to think about it is that the call to the new JLabel creates the JLabel object in memory immediately. 另一种思考方式是调用new JLabel立即在内存中创建JLabel对象。 Only after construction does the object get assigned to your JLabel label variable. 只有在构造之后,对象才会被分配给您的JLabel label变量。 The label variable is not your JLabel object. label变量不是您的JLabel对象。 It is a reference, a name that can take us to the JLabel object. 它是一个引用,可以将我们带到JLabel对象的名称。 The JLabel object itself is floating around in memory someplace. JLabel对象本身在内存中浮动。

Astronaut Tether 宇航员系绳

The JLabel object is like an astronaut on a spacewalk. JLabel对象就像太空行走上的宇航员。 Assigning that object to a label variable is like putting the end of the astronaut's tether in your hand. 将该对象分配给label变量就像将宇航员的系绳末端放在手中一样。 Using that label variable is like tugging on that tether to get to the astronaut. 使用该label变量就像拉扯该系绳以获得宇航员一样。

Your alternate code passes a reference to the JLabel object off to the JFrame object. 您的备用代码将对JLabel对象的引用传递给JFrame对象。 Again, the JLabel object already exists wholly and intact before the hand-off to the JFrame. 同样,在切换到JFrame 之前 ,JLabel对象已经完整且完整地存在 After the hand-off, the local code has "forgotten" about the JLabel. 在交接之后,本地代码已经“忘记”了JLabel。 No local reference variable means no way to get back to the JLabel object. 没有本地引用变量意味着无法返回JLabel对象。 The JLabel object still exists in memory some place, but is out of reach to our local code. JLabel对象在某些地方仍然存在于内存中,但是我们的本地代码无法访问。 In our spacewalk analogy, handing the reference off to the JFrame is like handing over the astronaut's tether to somebody else. 在我们的太空行走类比中,将参考文献交给JFrame就像将宇航员的绳索交给其他人一样。

If we kept a local JLabel reference variable and handed off to the JFrame, that would be like hooking up two tethers to our space-walking astronaut. 如果我们保留一个本地JLabel参考变量传递给JFrame,那就像把我的太空行走的宇航员连接两条绳索。 You hold one tether in your hand (the local var) while passing the second tether to someone else (the JFrame object). 在将第二个系绳传递给其他人(JFrame对象)时,您手持一条系绳(局部变量)。

If all the references are set to null or go out of scope, that is like releasing all the tethers to our astronaut. 如果所有引用都设置为null或超出范围,那就像释放所有系绳到我们的宇航员。 The object still lives in memory for a while but becomes a candidate for garbage collection. 该对象仍然存在于内存中一段时间​​,但成为垃圾收集的候选对象。 Actual removal from memory happens later, when the garbage collector runs and decides it is time to recycle that memory space. 实际从内存中删除发生在垃圾收集器运行时,并决定是时候回收该内存空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在引用Java对象时,两者之间有什么区别? - In referencing a java object, what is the difference between the two? java中这两个对象初始化有什么区别? - what is the difference between these two object initialization in java? 这两种创建 TreeMap(或任何地图)的方式有什么区别? - What is the difference between these two ways of creating a TreeMap (or any map)? 这两种从LocalDateTime创建DateTime的方法有什么区别? - What's the difference between these two ways of creating a DateTime from a LocalDateTime? 两者有什么区别? - What is the difference between the two? 只是声明对象数组和实际创建它的实例有什么区别? - What is the difference between just declaring array of object and actually creating instance of it? findFragmentById()和在Android中创建Fragment对象之间有什么区别? - What is difference between findFragmentById() and creating an object of Fragment in Android? 创建数组对象和使用花括号分配数组有什么区别? - What is the difference between creating an array object and assigning arrays with curly braces? 在java中获得两个集合之间的对称差异的最佳方法是什么? - What is the best way get the symmetric difference between two sets in java? 创建两个POJO的差异 object - Creating a difference object of two POJOs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM