这些是什么类型的java构造函数?构造函数链?
这些来自 github 上的 spring amqp 示例 https://github.com/SpringSource/spring-amqp-samples.git 这些是什么类型的java构造函数?它们是 getter 和 setter 的简写吗?
public class Quote {
public Quote() {
this(null, null);
}
public Quote(Stock stock, String price) {
this(stock, price, new Date().getTime());
}
与这个相反
public class Bicycle {
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}
These are from the spring amqp samples on github at
https://github.com/SpringSource/spring-amqp-samples.git
what type of java constructors are these? are they a short hand for getters and setters?
public class Quote {
public Quote() {
this(null, null);
}
public Quote(Stock stock, String price) {
this(stock, price, new Date().getTime());
}
as oppossed to this one
public class Bicycle {
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这些构造函数被重载以使用
this(...)
调用另一个构造函数。第一个无参数构造函数使用空参数调用第二个构造函数。第二个调用第三个构造函数(未显示),该构造函数必须采用Stock
、String
和long
。这种模式称为构造函数链接,通常用于提供多种实例化对象的方法,而无需重复代码。参数较少的构造函数会使用默认值填充缺少的参数,例如使用 new Date().getTime() ,或者仅传递 null 。请注意,必须至少有一个构造函数不调用
this(...)
,而是提供对super(...)< 的调用/code> 后面是构造函数的实现。当构造函数的第一行既没有指定
this(...)
也没有指定super(...)
时,对super( 的无参数调用)
是隐含的。因此,假设
Quote
类中没有更多的构造函数链接,则第三个构造函数可能如下所示:另请注意,对
this(...)
的调用仍然可以接下来是实现,尽管这偏离了链接模式:These constructors are overloaded to call another constructor using
this(...)
. The first no-arg constructor calls the second with null arguments. The second calls a third constructor (not shown), which must take aStock
,String
, andlong
. This pattern, called constructor chaining, is often used to provide multiple ways of instantiating an object without duplicate code. The constructor with fewer arguments fills in the missing arguments with default values, such as withnew Date().getTime()
, or else just passesnull
s.Note that there must be at least one constructor that does not call
this(...)
, and instead provides a call tosuper(...)
followed by the constructor implementation. When neitherthis(...)
norsuper(...)
are specified on the first line of a constructor, a no-arg call tosuper()
is implied.So assuming there isn't more constructor chaining in the
Quote
class, the third constructor probably looks like this:Also note that calls to
this(...)
can still be followed by implementation, though this deviates from the chaining pattern:这就是我们所说的伸缩模式。但是您在 Quote 类中使用的方式没有用。举个例子,假设在您的班级中,您有一个必需的属性和两个可选的属性。在这种情况下,您需要提供具有所需属性的构造函数,然后在该构造函数中您需要使用可选参数的默认值调用其他构造函数。
我从有效的java版本2中提取了这个java代码。
This is what we are calling telescoping pattern. But the way you used in Quote class is not useful. For an example think that in your class you have one required property and two optional properties. In this case you need to provide a constructor with that required property and then within that constructor you need to call other constructors with the default values of the optional parameters.
I extract this java code from effective java edition 2.
它正在调用一个带有整数参数的构造函数。
这是我在 Java 教程网页上找到的内容:
在本例中,它从 ArrayList 类中调用某些内容。这也在 Java 教程部分:
因此,您在本例中看到的是显式构造函数调用
来源:https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html
It's calling a constructor that takes an integer argument.
This is something I found on the Java Tutorials Webpage:
In this case it's calling something from the ArrayList class. This was also in the Java Tutorials section:
So what your seeing in this case is an Explicit Constructor Invocation
Source: https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html