我完全不熟悉Java,就像我昨天开始的那样。初学者Q:Java中的可选参数
我有一个类,我想有两个构造函数:一个没有参数,一个。
按说这应该是简单的:
public class sortList {
public int ncell, npart, cell_n, index, Xref;
// constructor(s):
public void sortList() {
initLists(1, 1);
}
public void sortList(int ncell_in, int npart_in) {
initLists(ncell_in, npart_in);
}
private void initLists(int ncell_in, int npart_in) {
/* DO STUFF */
}
}
当我把这个从我的main()虽然:
sortList mySL = new sortList(5, 6);
... java的抱怨:
写两种方法重载的构造myDSMC.java:5: error: constructor sortList in class sortList cannot be applied to given types;
sortList mySL = new sortList(5, 6);
^ required: no arguments
found: int,int
reason: actual and formal argument lists differ in length
1 error
(对于好奇心,我只是翻译了一个超级简单的DSMC代码从C++ ...)。
我错过了什么愚蠢的东西?
谢谢。 -Peter
备注:Java的近似通用风格的一部分是**以大写字母**开头的类名。您的构造函数与您的类具有相同的名称,并且也将被大写。所有其他方法都以小写字母开头。 –