Jon*_*eet 11
方法没有构造函数......类.例如:
public class Dummy
{
private int credits;
private int contactHours;
private String name;
public Dummy()
{
name = "";
credits = 0;
contactHours = 0;
}
// More stuff here, e.g. property accessors
}
Run Code Online (Sandbox Code Playgroud)
您实际上不必设置credits或contactHours,因为int字段默认为0,无论如何.
您可能想要至少一个带有初始值的构造函数 - 在这种情况下,您的无参数构造函数可以委托给它:
public class Dummy
{
private String name;
private int credits;
private int contactHours;
public Dummy()
{
this("", 0, 0);
}
public Dummy(String name, int credits, int contactHours)
{
this.name = name;
this.credits = credits;
this.contactHours = contactHours;
}
// More stuff here, e.g. property accessors
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31525 次 |
| 最近记录: |