public Patient(String ptNo, String ptName, int procType) throws IOException
{
Patient.patientNo = ptNo;
Patient.patientName = ptName;
Patient.procedureType = procType;
}
仅当所有三个变量都是这样的静态时,此代码才有效
public class Patient
{
static String patientNo;
static int procedureType;
static String patientName;
public Patient(String ptNo, String ptName, int procType) throws IOException
{
Patient.patientNo = ptNo;
Patient.patientName = ptName;
Patient.procedureType = procType;
}
}
而像这样的东西
public Patient(String ptNo, String ptName, int procType) throws IOException
{
this.patientNo = ptNo;
this.patientName = ptName;
this.procedureType = procType;
}
将适用于非静态变量,“this”只是用于避免混淆,表示正在调用其构造函数的对象。例子
Patient p = new Patient("A","B",1);
就像说
p.patientNo = ptNo;
p.patientName = ptName;
p.procedureType = procType;
编辑
“this”在这种情况下来救援
public class Patient
{
String patientNo;
int procedureType;
String patientName;
public Patient(String patientNo, String patientName, int procedureType) throws IOException
{
this.patientNo = patientNo; // variables in LHS are class members, RHS are method parameters
this.patientName = patientName;
this.procedureType = procedureType;
}
}