我正在使用构造函数:
Donor(String lastName, String firstName, String type, int age, double minutes)
并且知道我必须将其命名为与它所在的公共类相同的名称,但是当我移动花括号使其包含在公共Donor类中时,我会遇到一些错误。
我的代码如下:
public class Program5
{
public static void main(String args[])
{
Arrays.sort(Donor, new lastNameComparator());
for (int i = 0; i < donor.length; i++)
{
System.out.println(Donor[i].getLastName());
}
Arrays.sort(Donor, new firstNameComparator());
for (int i = 0; i < donor.length; i++)
{
System.out.println(Donor[i].getFirstName());
}
Arrays.sort(Donor, new typeComparator());
for (int i = 0; i < donor.length; i++)
{
System.out.println(Donor[i].getType());
}
Arrays.sort(Donor, new ageComparator());
for (int i = 0; i < donor.length; i++)
{
System.out.println(Donor[i].getAge());
}
Arrays.sort(Donor, new minutesComparator());
for (int i = 0; i < donor.length; i++)
{
System.out.println(Donor[i].getMinutes());
}
}
public ArrayList<Donor> donorCSVList(String filePath) throws IOException
{
ArrayList<Donor> list = new ArrayList<Donor>();
Scanner scan = new Scanner(new File(filePath));
while (scan.hasNextLine())
{
String line = scan.nextLine();
String [] lineArray = line.split(",");
list.add(new Donor(lineArray[0], lineArray[1], lineArray[2], lineArray[3], lineArray[4]));
}
}
public class Donor
{
private String lastName;
private String firstName;
private String type;
private int age;
private double minutes;
}
public void Donor()
{
super();
type = "Not assigned";
age = 0;
minutes = 0.0;
}
Donor(String lastName, String firstName, String type, int age, double minutes)
{
super(lastName, firstName);
this.lastName = lastName;
this.firstName = firstName;
this.type = type;
this.age = age;
this.minutes = minutes;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getType()
{
return type;
}
public void setType(String type)
{
this.type = type;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public double setMinutes()
{
return minutes;
}
public void getMinutes(double minutes)
{
this.minutes = minutes;
}
public class lastNameComparator implements Comparator
{
public String compare(Donor o1, Donor o2)
{
String lastName1 = o1.getLastName();
String lastName2 = o2.getLastName();
return lastName1.compareTo(lastName2);
}
}
public class firstNameComparator implements Comparator
{
public String compare(Donor o1, Donor o2)
{
String firsName1 = o1.getFirstName();
String firstName2 = o2.getFirstName();
return firstName1.compareTo(firstName2);
}
}
public class typeComparator implements Comparator
{
public string compare(Donor o1, Donor o2)
{
String type1 = o1.getType();
String type2 = o2.getType();
return type1.compareTo(type2);
}
}
public class ageComparator implements Comparator
{
public int compare(Donor o1, Date o2)
{
int age1 = o1.getAge();
int age2 = o2.getAge();
//return o2.getAge() - o1.getAge();
}
}
public class minutesComparator implements Comparator
{
public double compare(Donor o1, Donor o2)
{
double minutes1 = o1.getMinutes();
double minutes2 = o2.getMinutes();
//return o2.getMinutes() - o1.getMinutes();
}
}
}
我的错误是:
Program5.java:66: error: invalid method declaration; return type required
Donor(String lastName, String firstName, String type, int age, double minutes)
最佳答案
您的主要问题是您已将Donor
类的大部分内容放错了位置。您所拥有的结构如下:
public class Donor
{
private String lastName;
}
public String getLastName()
{
return lastName;
}
虽然您想要的更像是这样:
public class Donor
{
private String lastName;
public String getLastName()
{
return lastName;
}
}
该类的所有成员都必须在花括号内,该花括号界定了类的内容,而不仅仅是字段。
另外,您似乎在构造函数中使用参数调用
super()
,该构造函数用于没有超类且带有参数的构造函数的类。那没有道理。最后,您可能打算公开您的
Donor
构造函数:public Donor(String lastName, ...