[英]Optional parameters: Multiple constructors cause errors
Since in Java optional parameters are not possible, I tried to create 2 constructors.由于在 Java 可选参数是不可能的,我尝试创建 2 个构造函数。
public class Tts {
public Context context;
private final MediaPlayer _mediaPlayer;
private final CopyBarVisualizer _barTop;
private final CopyBarVisualizer _barBottom;
public Tts(Context context) {
this.context = context;
}
public Tts(Context context, MediaPlayer _mediaPlayer, BarVisualizer _barTop, BarVisualizer _barBottom) {
this.context = context;
this._mediaPlayer = _mediaPlayer;
this._barTop = (CopyBarVisualizer) _barTop;
this._barBottom = (CopyBarVisualizer) _barBottom;
}
}
Now my problems are the private properties.现在我的问题是私有财产。 There I get the error
我得到了错误
Variable '_mediaPlayer' might not have been initialized
变量“_mediaPlayer”可能尚未初始化
In another method, I want to check if the properties are set.在另一种方法中,我想检查是否设置了属性。 But how can I avoid these errors?
但是我怎样才能避免这些错误呢?
public class Tts {
public Context context;
private MediaPlayer _mediaPlayer;
private CopyBarVisualizer _barTop;
private CopyBarVisualizer _barBottom;
public Tts(Context context) {
this.context = context;
}
public Tts(Context context, MediaPlayer _mediaPlayer, BarVisualizer _barTop, BarVisualizer _barBottom) {
this.context = context;
this._mediaPlayer = _mediaPlayer;
this._barTop = (CopyBarVisualizer) _barTop;
this._barBottom = (CopyBarVisualizer) _barBottom;
}
}
public class Tts {
public Context context;
private final MediaPlayer _mediaPlayer;
private final CopyBarVisualizer _barTop;
private final CopyBarVisualizer _barBottom;
public Tts(Context context) {
this(context, null, null, null); // to solve the error problem
}
public Tts(Context context, MediaPlayer _mediaPlayer, BarVisualizer _barTop, BarVisualizer _barBottom) {
this.context = context;
this._mediaPlayer = _mediaPlayer;
this._barTop = (CopyBarVisualizer) _barTop;
this._barBottom = (CopyBarVisualizer) _barBottom;
}
}
This code is from @Khahani此代码来自@Khahani
public class TtsBuilder{
public Context context;
private MediaPlayer _mediaPlayer = null;
private BarVisualizer _barTop = null;
private BarVisualizer _barBottom = null;
public TtsBuilder(Context context) {
this.context = context;
}
public TtsBuilder withMediaPlayer(MediaPlayer _mediaPlayer) {
this._mediaPlayer = _mediaPlayer;
return this;
}
public TtsBuilder withBarTop(BarVisualizer _barTop) {
this._barTop = _barTop;
return this;
}
public TtsBuilder withBarBottom(BarVisualizer _barBottom) {
this._barBottom = _barBottom;
return this;
}
public Tts build() {
return new Tts(context, _mediaPlayer, _barTop, _barBottom);
}
}
public static void main(String[] args){
Tts ttsMedia = new Tts(aContext).withMediaPlayer(aMediaPlayer).build();
Tts ttsBarTopBottom = new Tts(aContext)
.withBarTop(aBarTop)
.withBarBottom(aBarBottom)
.build();
}
The naming convention in Java recommends: Java 中的命名约定建议:
Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed.
变量名称不应以下划线 _ 或美元符号 $ 字符开头,即使两者都允许。
For more details you can check Oracle documentation .有关更多详细信息,您可以查看Oracle 文档。
If you want to solve the error problem, you can fix it with the code below;如果你想解决错误问题,你可以用下面的代码修复它; however, when the constructor initializes final fields, you can't change them, so probably it's better to remove the final keyword.
但是,当构造函数初始化 final 字段时,您无法更改它们,因此最好删除 final 关键字。
public class Tts {
public Context context;
private final MediaPlayer _mediaPlayer;
private final CopyBarVisualizer _barTop;
private final CopyBarVisualizer _barBottom;
public Tts(Context context) {
this(context, null, null, null); // to solve the error problem
}
public Tts(Context context, MediaPlayer _mediaPlayer, BarVisualizer _barTop, BarVisualizer _barBottom) {
this.context = context;
this._mediaPlayer = _mediaPlayer;
this._barTop = (CopyBarVisualizer) _barTop;
this._barBottom = (CopyBarVisualizer) _barBottom;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.