我正在尝试使用给定的名称和性别创建一个新的Couple实例,并在addCouple方法中添加到couple的集合。我有另一个名为Couple的课程。在那个课程中,我有一个名字和性别的getter和Setters。我尝试使用批量操作列表:
List<Dating> list1 = new ArrayList<Dating>(this.addCouple);
但我得到一个错误“非静态变量,这不能从静态上下文中引用”。然后我尝试使用Collection.sort
然后打印出列表。我收到了同样的错误消息。所以我相信我不知道如何使用this.addList
。有人能告诉我如何使用它。我应该使用this.addList
吗?提前致谢。
public class Dating
{
private List<Male> maleList;
private List<Female> femaleList;
public Dating()
{
super();
maleList = new ArrayList<Single>();
femaleList = new ArrayList<Single>();
}
public void lists()
{
this.addList("Jack","Male",'m');
this.addList("Mike","Male",'m');
this.addList("Lynda","Female",'f');
this.addList("Katie","Female",'f');
}
public static void addCouple (String aName, char aGender)
{
Collections.sort(this.addList);
for (Couple group : this.addList)
{
System.out.println(" " + group.getName() + " " + group.getGender());
}
}
答案 0 :(得分:8)
我认为你有比这更大的问题。男,女,单身?这些是Person类的属性,而不是类本身的属性。您的设计需要返工。
你需要人和夫妻课程。您还需要一个反映现实世界的模型。无论喜欢与否,你都可以拥有男性 - 男性和女性 - 女性伴侣。
我是这样写的。如您所见,List工作正常:Gender.java:
public enum Gender
{
MALE, FEMALE;
}
MaritalStatus.java:
public enum MaritalStatus
{
SINGLE, MARRIED, DIVORCED, WIDOWED, SEPARATED;
}
Person.java:
import java.text.DateFormat;
import java.util.Date;
public class Person
{
private String name;
private Date birthdate;
private Gender gender;
private MaritalStatus maritalStatus;
public Person(String name)
{
this(name, new Date(), Gender.MALE, MaritalStatus.SINGLE);
}
public Person(String name, Gender gender)
{
this(name, new Date(), gender, MaritalStatus.SINGLE);
}
public Person(String name, Date birthdate)
{
this(name, birthdate, Gender.MALE, MaritalStatus.SINGLE);
}
public Person(String name, Date birthdate, Gender gender)
{
this(name, birthdate, gender, MaritalStatus.SINGLE);
}
public Person(String name, Date birthdate, Gender gender, MaritalStatus maritalStatus)
{
if ((name == null) || (name.trim().length() == 0))
throw new IllegalArgumentException("name cannot be blank or null");
this.name = name;
this.birthdate = ((birthdate == null) ? new Date() : new Date(birthdate.getTime()));
this.gender = gender;
this.maritalStatus = maritalStatus;
}
public String getName()
{
return name;
}
public Date getBirthdate()
{
return new Date(birthdate.getTime());
}
public Gender getGender()
{
return gender;
}
public MaritalStatus getMaritalStatus()
{
return maritalStatus;
}
@Override
public boolean equals(Object o)
{
if (this == o)
{
return true;
}
if (o == null || getClass() != o.getClass())
{
return false;
}
Person person = (Person) o;
if (!birthdate.equals(person.birthdate))
{
return false;
}
if (gender != person.gender)
{
return false;
}
if (maritalStatus != person.maritalStatus)
{
return false;
}
if (!name.equals(person.name))
{
return false;
}
return true;
}
@Override
public int hashCode()
{
int result = name.hashCode();
result = 31 * result + birthdate.hashCode();
result = 31 * result + gender.hashCode();
result = 31 * result + maritalStatus.hashCode();
return result;
}
@Override
public String toString()
{
return "Person{" +
"name='" + name + '\'' +
", birthdate=" + DateFormat.getDateInstance(DateFormat.MEDIUM).format(birthdate) +
", gender=" + gender +
", maritalStatus=" + maritalStatus +
'}';
}
}
Couple.java:
public class Couple
{
private Person ying;
private Person yang;
public Couple(Person ying, Person yang)
{
this.ying = ying;
this.yang = yang;
}
public Person getYing()
{
return ying;
}
public Person getYang()
{
return yang;
}
@Override
public String toString()
{
return "Couple{" +
"ying=" + ying +
", yang=" + yang +
'}';
}
}
Dating.java:
import java.util.ArrayList;
import java.util.List;
public class Dating
{
private List<Person> individuals = new ArrayList<Person>();
private List<Couple> couples = new ArrayList<Couple>();
public static void main(String[] args)
{
Dating dating = new Dating();
dating.addIndividual(new Person("Jack"));
dating.addIndividual(new Person("Mike"));
dating.addIndividual(new Person("Lydia", Gender.FEMALE));
dating.addIndividual(new Person("Kate", Gender.FEMALE));
System.out.println(dating);
}
public void addIndividual(Person p)
{
this.individuals.add(p);
}
public void removeIndividual(Person p)
{
this.individuals.remove(p);
}
public Person findIndividual(String name)
{
Person found = null;
if (name != null)
{
for (Person p : this.individuals)
{
if (name.equals(p.getName()))
{
found = p;
break;
}
}
}
return found;
}
@Override
public String toString()
{
return "Dating{" +
"individuals=" + individuals +
", couples=" + couples +
'}';
}
}
答案 1 :(得分:2)
您的addCouple
被声明为静态方法。静态方法没有this
,因为它们不属于实例。他们属于一个班级。
答案 2 :(得分:1)
您必须使您的成员保持静态,或者必须从您的函数中删除静态标志。粗略地说,你不能将静态与静态混合。
答案 3 :(得分:1)
你最好创建一个“Person”类,它可能大致如下:
public class Person
{
private String name;
private boolean male; // or use an enum GENDER if you plan to do more than male/female
public Person(String name, boolean male)
{
this.name = name;
this.male = male;
}
// [...] getters and setters
public String toString()
{
return name + " " + (male ? "male" : "female");
}
}
并且,取决于你真正想做的事情......(更多信息会更好)
public class Dating
{
private List<Person> persons;
//[...] constructor
public void addWhateverYouMeanWithCouple(String name, boolean male)
{
this.persons.add(new Person(name, male));
System.out.println(persons);
}
}
我真的不知道你在寻找什么,这是我能为你提供的唯一代码,不过我仍然认为这不是你想要的。
如果要对人员列表进行排序,您将在Comparable<Person>
类中实现Person
(请参阅Comparable JavaDoc)并提供一些逻辑来比较两个人(例如使用{{ 1}})。
如果你真的想要“情侣”或对,请看HashMap。