gpt4 book ai didi

java - 创建对象时是否可以将属性设置为可选?

转载 作者:行者123 更新时间:2023-12-01 07:01:11 24 4
gpt4 key购买 nike

好的,我有这门课

public DBRestorerWorker(String dbName, Path fromFile, Path targetDataPath, Path targetLogPath,
ProgressBar bar, Properties properties, Runnable done) {
this.dbName = dbName;
this.targetDataPath = targetDataPath;
this.from = fromFile;
this.targetLogPath = targetLogPath;
this.bar = bar;
this.properties = properties;
this.done = done;
}

此类在应用程序中使用,现在我正在创建它的精简版本,因此我仍然需要使用这部分,但在精简版本中没有 ProgressBar,但我仍然需要创建此类的对象,因此有没有办法让 ProgressBar 成为可选的,因为旧版本仍然需要它。

最佳答案

您可以添加一个新的构造函数,该构造函数只需将 null 分配给 ProgressBar 字段:

public DBRestorerWorker(String dbName, Path fromFile, Path targetDataPath,
Path targetLogPath, Properties properties, Runnable done) {

DBRestorerWorker(dbName, fromFile, targetDataPath, targetLogPath, null,
properties, done);
}

如果由于某种原因无法修改原始类,那么您仍然可以扩展以前的类并添加此构造函数。

关于java - 创建对象时是否可以将属性设置为可选?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49535790/

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