Java 作业问题。。。

2025-04-06 08:34:23
推荐回答(1个)
回答1:

其实partner就是一个属性罢了,如果一个人partner不为空那么就不能再marry()结婚了,不是吗?
下面是我写的 看看吧,或许有帮助~
public class Person {

private String name;
private int age;
private boolean gender;
private Person partner;

public Person(){
super();
}
public Person(String name, int age, boolean gender) {
this.name = name;
this.age = age;
this.gender = gender;
}

public void marry(Person p){
if(partner!=null){
System.out.println("人家已经结婚了(>_<)");
return;
}
if(p.getGender()==getGender()){
System.out.println("不提倡同性恋!");
return ;
}
if(p.getGender()==true&&p.getAge()<24||p.getGender()==false&&p.getAge()<22){
System.out.println(p.getName()+"年龄不到呢");
return ;
}
if(getGender()==true&&getAge()<24||getGender()==false&&getAge()<22){
System.out.println(getName()+"年龄不到呢");
return ;
}
System.out.println("喜结良缘");
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean getGender() {
return gender;
}
public void setGender(boolean gender) {
this.gender = gender;
}
public Person getPartner() {
return partner;
}
public void setPartner(Person partner) {
this.partner = partner;
}

}