写一个Person类,定义人的基本属性:name(姓名)、age(年龄)、gender(性别)、partner(配偶)
建立Person类,提供私有属性name(姓名)、age(年龄)、gender(性别)、partner(配偶),其中gender属性的类型是boolean(true表示男...
建立Person类, 提供私有属性name(姓名)、age(年龄)、gender(性别)、partner(配偶),其中gender属性的类型是boolean(true表示男性,false表示女性),partner(是Person类型,指定另外一个人——配偶),提供属性对应的getter与setter方法
提供isMarry()返回类型为boolean,如果能结婚 返回为true,否则返回为false
建立测试类MarryTest,创建多个Person类的实例
我这样写对么
public class Person {
private String name;
private int age;
private boolean gender;
private Person partner;
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isGender() {
return gender;
}
public void setGender(boolean gender) {
this.gender = gender;
}
public Person getPartner() {
return partner;
}
public void setPartner(Person partner) {
this.partner = partner;
}
public Person(String name,int age,boolean gender)
{
this.name=name;
this.age=age;
this.gender=gender;
}
if(partner==null&&gender!=partner.isGender())
{
return true;
}else {
return false;
}
}
}
----------------------------------------------------------------------------------
public class MarryTest {
public static void main(String[] args) {
Person p=new Person("张三", 20, true);
Person p1= new Person("李四", 20, false);
p.setPartner(p1);
Person p2= new Person("王五", 20, true);
Person p3= new Person("马六", 20, false);
p2.setPartner(p3);
System.out.println(p2.isMarry());
}
} 展开
提供isMarry()返回类型为boolean,如果能结婚 返回为true,否则返回为false
建立测试类MarryTest,创建多个Person类的实例
我这样写对么
public class Person {
private String name;
private int age;
private boolean gender;
private Person partner;
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isGender() {
return gender;
}
public void setGender(boolean gender) {
this.gender = gender;
}
public Person getPartner() {
return partner;
}
public void setPartner(Person partner) {
this.partner = partner;
}
public Person(String name,int age,boolean gender)
{
this.name=name;
this.age=age;
this.gender=gender;
}
if(partner==null&&gender!=partner.isGender())
{
return true;
}else {
return false;
}
}
}
----------------------------------------------------------------------------------
public class MarryTest {
public static void main(String[] args) {
Person p=new Person("张三", 20, true);
Person p1= new Person("李四", 20, false);
p.setPartner(p1);
Person p2= new Person("王五", 20, true);
Person p3= new Person("马六", 20, false);
p2.setPartner(p3);
System.out.println(p2.isMarry());
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询