Java 如何创建构造函数?
我有以下代码:Java 如何创建构造函数?,java,computer-science,Java,Computer Science,我有以下代码: public class BigVic extends Looper { private int itsNumFilled; // count this's filled slots private int itsNumEmpty; // count this's non-filled slots public BigVic() // constructor { // left as an exercise } //==
public class BigVic extends Looper
{
private int itsNumFilled; // count this's filled slots
private int itsNumEmpty; // count this's non-filled slots
public BigVic() // constructor
{
// left as an exercise
} //======================
/** Tell how many of the executor's slots are filled. */
public int getNumFilled()
{
return itsNumFilled;
} //======================
/** Tell how many of the executor's slots are empty. */
public int getNumEmpty()
{
return itsNumEmpty;
} //======================
/** Do the original putCD(), but also update the counters. */
public void putCD()
{
if ( ! seesCD() && stackHasCD())
{
itsNumFilled++;
itsNumEmpty--;
super.putCD();
}
} //======================
/** Do the original takeCD(), but also update the counters. */
public void takeCD()
{
if (seesCD())
{
itsNumFilled--;
itsNumEmpty++;
super.takeCD();
}
} //======================
}
我应该这样做:
写出所需的BigVic构造函数
我终于想出了一个办法:
public BigVic()
{
super();
this.putCD();
this.takeCD();
}
我如何回答这个问题?我不知道除了在构造函数中添加我已经拥有的东西,我还能做些什么来完成它?我需要的东西已经在里面了吗?据我所知,看起来你想用它
public BigVic() // constructor
{
this.itsNumFilled = 0; // Nothing is filled.
this.itsNumEmpty = 100; // For a default size of 100 executor "slots".
} //======================
提示:构造函数通常用于初始化类中的变量。所以我的构造函数中唯一缺少的是getNumFilled和getNumEmpty?这听起来很对吗?getter只会返回一个值,您仍然需要存储它。通常是直接完成的。看起来你必须再次复习笔记。例如,如果你有私有的int i;在构造函数中用i=5初始化它;不是必需的-如果您不编写它,它是隐含的。至于需要什么,你还没有给出任何信息。我会继续,我觉得这是正确的。谢谢D