其他分享
首页 > 其他分享> > 类的练习

类的练习

作者:互联网

class Husband{
    String name;
    String id;
    String brith;
    Wife wife;
    public Husband(){
        
    }
    public Husband(String s1, String s2, String s3, Wife w){
        name = s1;
        id = s2;
        brith = s3;
        wife = w;
    }
}
class Wife{
    String name;
    String id;
    String brith;
    Husband husband;
    public Wife(){
        
    }
    public Wife(String s1, String s2, String s3, Husband h){
        name = s1;
        id = s2;
        brith = s3;
        husband = h;
    }
}
public class Last{
    public static void main(String[] args){
        Husband x = new Husband("张三", "123", "456", null);
        Wife y = new Wife("李四", "987", "654", null);
        x.wife = y;
        y.husband = x;
        System.out.println(x.name +"的老婆是"+ x.wife.name);
        System.out.println(y.name +"的老公是"+ y.husband.name);
    }
}
//张三的老婆是李四
//李四的老公是张三

类创建的基础练习

标签:String,Wife,练习,brith,public,Husband,name
来源: https://www.cnblogs.com/kelvin-liu/p/16396409.html