这些是什么类型的java构造函数?构造函数链?

发布于 2025-01-03 11:11:13 字数 631 浏览 1 评论 0原文

这些来自 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 技术交流群。

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

发布评论

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

评论(3

燕归巢 2025-01-10 11:11:13

这些构造函数被重载以使用 this(...) 调用另一个构造函数。第一个无参数构造函数使用空参数调用第二个构造函数。第二个调用第三个构造函数(未显示),该构造函数必须采用 StockStringlong。这种模式称为构造函数链接,通常用于提供多种实例化对象的方法,而无需重复代码。参数较少的构造函数会使用默认值填充缺少的参数,例如使用 new Date().getTime() ,或者仅传递 null 。

请注意,必须至少有一个构造函数不调用 this(...),而是提供对 super(...)< 的调用/code> 后面是构造函数的实现。当构造函数的第一行既没有指定 this(...) 也没有指定 super(...) 时,对 super( 的无参数调用) 是隐含的。

因此,假设 Quote 类中没有更多的构造函数链接,则第三个构造函数可能如下所示:

public Quote(Stock stock, String price, long timeInMillis) {
    //implied call to super() - the default constructor of the Object class

    //constructor implementation
    this.stock = stock;
    this.price = price;
    this.timeInMillis = timeInMillis;
}

另请注意,对 this(...) 的调用仍然可以接下来是实现,尽管这偏离了链接模式:

public Quote(Stock stock, String price) {
    this(stock, price, new Date().getTime());

    anotherField = extraCalculation(stock);
}

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 a Stock, String, and long. 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 with new Date().getTime(), or else just passes nulls.

Note that there must be at least one constructor that does not call this(...), and instead provides a call to super(...) followed by the constructor implementation. When neither this(...) nor super(...) are specified on the first line of a constructor, a no-arg call to super() is implied.

So assuming there isn't more constructor chaining in the Quote class, the third constructor probably looks like this:

public Quote(Stock stock, String price, long timeInMillis) {
    //implied call to super() - the default constructor of the Object class

    //constructor implementation
    this.stock = stock;
    this.price = price;
    this.timeInMillis = timeInMillis;
}

Also note that calls to this(...) can still be followed by implementation, though this deviates from the chaining pattern:

public Quote(Stock stock, String price) {
    this(stock, price, new Date().getTime());

    anotherField = extraCalculation(stock);
}
花开半夏魅人心 2025-01-10 11:11:13

这就是我们所说的伸缩模式。但是您在 Quote 类中使用的方式没有用。举个例子,假设在您的班级中,您有一个必需的属性和两个可选的属性。在这种情况下,您需要提供具有所需属性的构造函数,然后在该构造函数中您需要使用可选参数的默认值调用其他构造函数。

// Telescoping constructor pattern - does not scale well!
public class NutritionFacts {
private final int servingSize; // (mL)  required
private final int servings; // (per container) required
private final int calories; //  optional
private final int fat; // (g) optional
private final int sodium; // (mg) optional
private final int carbohydrate; // (g)   optional
public NutritionFacts(int servingSize, int servings) {
this(servingSize, servings, 0);
}
public NutritionFacts(int servingSize, int servings,
int calories) {
this(servingSize, servings, calories, 0);
}
public NutritionFacts(int servingSize, int servings,
int calories, int fat) {
this(servingSize, servings, calories, fat, 0);
}

我从有效的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.

// Telescoping constructor pattern - does not scale well!
public class NutritionFacts {
private final int servingSize; // (mL)  required
private final int servings; // (per container) required
private final int calories; //  optional
private final int fat; // (g) optional
private final int sodium; // (mg) optional
private final int carbohydrate; // (g)   optional
public NutritionFacts(int servingSize, int servings) {
this(servingSize, servings, 0);
}
public NutritionFacts(int servingSize, int servings,
int calories) {
this(servingSize, servings, calories, 0);
}
public NutritionFacts(int servingSize, int servings,
int calories, int fat) {
this(servingSize, servings, calories, fat, 0);
}

I extract this java code from effective java edition 2.

梦太阳 2025-01-10 11:11:13

它正在调用一个带有整数参数的构造函数。

这是我在 Java 教程网页上找到的内容:

您可以使用 this 从实例方法或构造函数中引用当前对象的任何成员。

在本例中,它从 ArrayList 类中调用某些内容。这也在 Java 教程部分:

在构造函数中,您还可以使用 this 关键字来调用同一类中的另一个构造函数。这样做称为显式构造函数调用。

因此,您在本例中看到的是显式构造函数调用

来源: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:

You can refer to any member of the current object from within an instance method or a constructor by using this.

In this case it's calling something from the ArrayList class. This was also in the Java Tutorials section:

From within a constructor, you can also use the this keyword to call another constructor in the same class. Doing so is called an explicit constructor invocation.

So what your seeing in this case is an Explicit Constructor Invocation

Source: https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html

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