题目:定义一个丈夫Husband类,有姓名、年龄、妻子属性
定义一个妻子Wife类,有姓名、年龄、丈夫属性
丈夫类中有一个getInfo方法,其中,能显示自己的姓名,年龄,和他的妻子的姓名,年龄
妻子类中有一个getInfo方法,其中,能显示自己的姓名,年龄,和她的丈夫的姓名,年龄
定义一个测试类,创建妻子和丈夫对象,然后测试
public class FamilyTest {
public static void main(String[] args) {
// 建立新的对象
Wife wife = new Wife("小红", 18);
Husband husband = new Husband("小明", 22);
wife.setHusband(husband);
husband.setWife(wife);
System.out.println(wife.getInfo());
System.out.println(husband.getInfo());
}
}
class Husband {
private String name;
private int age;
private Wife wife;
// 建立一个新的构造器
public Husband(String name, int age) {
this.name = name;
this.age = age;
}
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 Wife getWife() {
return wife;
}
public void setWife(Wife wife) {
this.wife = wife;
}
pu


1273

被折叠的 条评论
为什么被折叠?



