0

所以我正在创建一个程序,它有一个包含大约 20 个变量的类(我们称这个类为 Person)。当我尝试像这样初始化变量时:

Person Steph = new Person(SName, SAge, SPhysical, SJob, SEducation, SPastSecret, SSecret1, Secret2, SSecret3, SConnections, SLikes, SHates, SCondtion, SNAME, SAGE, SPHYSICAL, SJOB, SEDUCATION, SPASTSECRET, SSECRET1, SECRET2, SSECRET3, SCONNECTIONS);

我得到错误:

internal error; cannot instantiate Person.<init> at Person to 

然后列出变量。有谁知道是什么导致了这个错误?

编辑:这是 Person 的完整代码:

class Person extends Detect{
public String Name, Age, Physical, Job, Education, PastSecret, Secret1, Secret2, Secret3, Connections, Likes, Hates, Condition;
public boolean NAME, AGE, PHYSICAL, JOB, EDUCATION, PASTSECRET, SECRET1, SECRET2, SECRET3, CONNETCTIONS, LIKES, HATES, CONDITION;

public Person(String Name, String Age, String Physical, String Job, String Education, 
String PastSecret, String Secret1, String Secret2, String Secret3, String Connections, String Likes, 
String Hates, String Condition, boolean NAME, boolean AGE, boolean PHYSICAL, boolean JOB, boolean EDUCATION,
boolean PASTSECRET, boolean SECRET1, boolean SECRET2, boolean SECRET3, boolean CONNECTIONS, 
boolean LIKES, boolean HATES, boolean CONDITION) {

    this.Name = Name;
    this.Age = Age;
    this.Physical = Physical;
    this.Job = Job;
    this.Education = Education;
    this.PastSecret = PastSecret;
    this.Secret1 = Secret1;
    this.Secret2 = Secret2;
    this.Secret3 = Secret3;     
    this.Connections = Connections;
    this.Likes = Likes;
    this.Hates = Hates;
    this.Condition = Condition;
    this.NAME = NAME;
    this.AGE = AGE;
    this.PHYSICAL = PHYSICAL;
    this.JOB = JOB;
    this.EDUCATION = EDUCATION;
    this.PASTSECRET = PASTSECRET;
    this.SECRET1 = SECRET1;
    this.SECRET2 = SECRET2;
    this.SECRET3 = SECRET3;
    this.CONNECTIONS = CONNECTIONS;
    this.LIKES = LIKES;
    this.HATES = HATES;
    this.CONDITION = CONDITION;

}



public void File(){
    System.out.printf("Suspect Name: %s         Age: %s       Appearance: %s\n", Check(NAME, Name), Check(AGE, Age),
        Check(PHYSICAL, Physical));
    System.out.printf("Job: %s          Education: %s           Past Secret: %s\n", Check(JOB, Job), Check(EDUCATION, Education), 
        Check(PASTSECRET, PastSecret));
    System.out.printf("Connections: %s    Secret: %s      Secret: %s    Secret: %s\n", Check(CONNECTIONS, Connections), Check(SECRET1, Secret1),
        Check(SECRET2, Secret2), Check(SECRET3, Secret3));
    System.out.printf("Likes: %s    Hates: %s    Conditions: %s\n\n", Check(LIKES, Likes), Check(HATES, Hates), Check(CONDITIONS, Conditions));
}

}

4

3 回答 3

4

您将 23 个参数传递给具有 26 个参数的构造函数。

于 2012-07-19T16:09:58.963 回答
3

首先,不要让类构造函数传递20个变量。创建一个 POJO 对象并使用它。例如:

public class Structure {
   private Object someObj;

   private Object someObj2;

   private Object someObj3;

   private Object someObj4;

   //getters and setters
}

在将其发送到Person类构造函数之前,通过 setter 方法设置所需的变量并仅传递此类Structure。(这使您的代码更高效,对全球社区和您自己来说更具可读性。)

关于初始化。您确定Person该类的构造函数接受所有 20 个变量吗?我很确定您的Person类构造函数是错误的。尝试这种方式,这应该工作。

于 2012-07-19T16:10:18.867 回答
1

Person.<init>指 Person 类的构造函数。

该错误表明 Person 类的构造函数与您提供的参数不匹配。不幸的是,它没有告诉你为什么不这样做。

您可能希望下载并安装集成开发环境 (IDE),例如Eclipse。它将为您提供有关此类编程问题的更多信息,并使编码变得更加容易。

于 2012-07-19T16:13:05.303 回答