gpt4 book ai didi

java - 有没有办法在继承中设置构造函数

转载 作者:行者123 更新时间:2023-12-02 00:09:19 24 4
gpt4 key购买 nike

所以我想知道是否有一种方法可以为 bluej 上的构造函数设置某些内容就像将 boolean 值设置为 true 而不询问其参数

public SaleItem(String description,
double weight,
boolean canBePickedUp,
double cost)
{

// initialise instance variables
super(description,weight,(canBePickedUp = true));
this.cost = cost;
}

最佳答案

Java 不允许非指定参数使用默认值。您可以执行以下操作

public SaleItem(String description, double weight, double cost) {

// initialise instance variables
super(description,weight,true);
this.cost = cost;
}

您可以使用 canBePickedUp 参数执行第二个构造函数。

关于java - 有没有办法在继承中设置构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13182412/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com