丈夫和妻子的作业
/* 1.定义丈夫类 Husband 和妻子类 Wife 2.丈夫类属性包括:身份证号,姓名,出生日期,妻子 3.妻子类属性包括:身份证号,姓名,出生日期,丈夫 4.分别给这两个类提供构造方法,(无参数构造和有参数构造都要提供) 5.编写测试程序,创建丈夫对象,然后再创建妻子对象,丈夫对象关联妻子对象 妻子对象关联丈夫对象,要求能够输出这个“丈夫对象”的妻子的名字,或者能够 输出这个“妻子对象”的丈夫的名字 */ public class test01 { public static void main(String[] args) { //创建丈夫对象(此时丈夫还没有妻子) Husband h = new Husband("341182", "李四", "19981113"); //创建妻子对象(此时妻子还没有丈夫) Wife w = new Wife("342622", "王婆", "19971112"); //让丈夫和妻子发生关系 h.wife = w; w.husband = h; System.out.println(h.name+"的妻子是"+h.wife.name); System.out.println(w.name+"的丈夫是"+w.husband.name); } } //创建丈夫类 class Husband{ //属性:身份证,姓名,出生日期,妻子 String idcard; String name; String birth; Wife wife; //无参构造 public Husband(){ } //有参构造 public Husband(String s1,String s2,String s3){ idcard = s1; name = s2; birth = s3; } } class Wife{ String idcard; String name; String birth; Husband husband; //无参构造 public Wife(){ } //有参构造 public Wife(String s1,String s2,String s3){ idcard = s1; name = s2; birth = s3; } }