gpt4 book ai didi

java - java构造函数的默认参数

转载 作者:行者123 更新时间:2023-12-01 10:21:29 27 4
gpt4 key购买 nike

我在名为 PlayableFighter 的 java 类中使用此构造函数

public PlayableFighter(String name, int maxHealthPoints, int blastDamage, int physicalDamage,
int maxKi, int maxStamina, ArrayList<SuperAttack> superAttacks, ArrayList<UltimateAttack>
ultimateAttacks)

我想从一个名为 Earthling 的类中调用它,该类继承了 PlayableFighter 但我没有 superAttacks 或 UltimateAttacks 的值,所以我想将它们设置为默认值,我使用了 null 但有更好的方法吗?

public Earthling(){
super("Earthling",1250,50,50,4,4,null,null);
}

最佳答案

如果您可以管理 PlayableFighter 的代码,我建议添加另一个构造函数,该构造函数获取除“superAttacks”和“ultimateAttacks”之外的所有参数,这将使用默认参数调用其他构造函数,如下所示:

public PlayableFighter(String name, int maxHealthPoints, int blastDamage, int physicalDamage, int maxKi, int maxStamina){
this(name, maxHealthPoints, blastDamage, physicalDamage, maxKi, maxStamina, null, null);
}

关于java - java构造函数的默认参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35577320/

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