简单Java类之 一对多、 双向一对多、多对多

一对多

package practice;

class Privance{
    private int pid;
    private String pname;
    private City [] cities;
    
    public Privance(int pid,String pname){
        this.pid = pid;
        this.pname = pname;
    }
    public void setCities(City [] cities){
        this.cities = cities;
    }
    public City [] getCities(){
        return this.cities;
    }
    public String getInfo(){
        return "省份编号:"+this.pid+" "+"省份名称:"+this.pname;
    }
}

class City{
    private int cid;
    private String cname;
    private Privance privance;
    
    
    public City(int cid,String cname){
        this.cid = cid;
        this.cname = cname;
    }
    public void setPrivance(Privance privance){
        this.privance = privance;
    }
    public Privance getPrivance(){
        return privance;
    }
    
    public String getInfo(){
        return "城市编号:"+this.cid+" "+"城市名称:"+this.cname;
    }
}

public class province {
    public static void main(String args[]){
        Privance p = new Privance(1,"hebei");
        City c1 = new City(2,"dkkd");
        City c2 = new City(3,"kskd");
        City c3 = new City(4,"wskd");
        c1.setPrivance(p);
        c2.setPrivance(p);
        c3.setPrivance(p);
        p.setCities(new City [] {c1,c2,c3});
        System.out.println(p.getInfo());
        for(int x=0;x

双向一对多

package practice;

class Item{
    private int iid;
    private String iname;
    private String inote;
    private Type types [];
    private Pro pros []; //注意pros是对象 切记
    public Item(int iid,String iname,String inote){
        this.iid = iid;
        this.iname = iname;
        this.inote = inote;
    }
    public void setTypes(Type [] types){
        this.types = types;
    }
    public void setPros(Pro [] pros){
        this.pros = pros;
    }
    public Type [] getTypes(){
        return types;
    }
    public Pro [] getPros(){
        return pros;
    }
    public String getInfo(){
        return "IID:"+this.iid+" "+"INAME:"+this.iname+" "+"IDEC:"+this.inote;
    }
}
class Type{
    private int tid;
    private String tname;
    private String tnote;
    private Item item;
    private Pro [] pross;
    public Type(int tid,String tname,String tnote){
        this.tid = tid;
        this.tname = tname;
        this.tnote = tnote;
    }
    public void setItem(Item item){
        this.item = item;
    }
    public void setPross(Pro [] pross){
        this.pross = pross;
    }
    public Item getItem(){
        return this.item;
    }
    public Pro [] getPross(){
        return this.pross;
    }
    public String getInfo(){
        return "TID:"+this.tid+" "+"TNAME:"+this.tname+"TDEC:"+this.tnote;
    }
}
class Pro{
    private int pid;
    private String pname;
    private double price;
    private Item item;
    private Type type;
    public Pro(int pid,String pname,double price){
        this.pid = pid;
        this.pname = pname;
        this.price = price;
    }
    public void setItem(Item item){
        this.item = item;
    }
    public void setType(Type type){
        this.type = type;
    }
    public Item getItem(){
        return this.item;
    }
    public Type getType(){
        return this.type;
    }
    public String getInfo(){
        return "PID:"+this.pid+" "+"PNAME:"+this.pname+"PRICDE:"+this.price;
    }
}
public class Two {
    public static void main(String [] args){
        Item i = new Item(1,"学院","-");
        Type t1 = new Type(101,"教学一班","-");
        Type t2 = new Type(201,"教学二班","-");
        Type t3 = new Type(301,"教学三班","-");
        Pro p1 = new Pro(1011,"开始开",85);
        Pro p2 = new Pro(1012,"库萨克",96);
        Pro p3 = new Pro(2011,"卡卡啊",75);
        Pro p4 = new Pro(2012,"卡阿卡",45);
        Pro p5 = new Pro(3011,"水口山",25);
        Pro p6 = new Pro(3012,"上课考",15);
        i.setTypes(new Type [] {t1,t2,t3});
        t1.setItem(i);
        t2.setItem(i);
        t3.setItem(i);
        
        p1.setItem(i);
        p2.setItem(i);
        p3.setItem(i);
        p4.setItem(i);
        p5.setItem(i);
        p6.setItem(i);
        i.setPros(new Pro [] {p1,p2,p3,p4,p5,p6});
        
        t1.setPross(new Pro [] {p1,p2});
        t2.setPross(new Pro [] {p3,p4});
        t3.setPross(new Pro [] {p5,p6});
        p1.setType(t1);
        p2.setType(t1);
        p3.setType(t2);
        p4.setType(t2);
        p5.setType(t3);
        p6.setType(t3);
        
        System.out.println(i.getInfo());
        for(int h=0;h

多对多

package practice;

class Admin{
    private int id;
    private String password;
    private Role role;
    public Admin(int id,String password){
        this.id = id;
        this.password = password;
    }
    public void setRole(Role role){
        this.role = role;
    }
    public Role getRole(){
        return this.role;
    }
    public String getInfo(){
        return "管理员ID:"+this.id+" "+"管理员密码:"+this.password;
    }
}
class Role{
    private int rid;
    private String rname;
    private Admin []admins;
    private Group [] groups;
    public Role(int rid,String rname){
        this.rid = rid;
        this.rname = rname;
    }
    public void setGtoups(Group [] groups){
        this.groups = groups;
    }
    public Group [] getGroups(){
        return this.groups;
    }
    public void setAdmins(Admin [] admins){
        this.admins = admins;
    }
    public Admin [] getAdmins(){
        return this.admins;
    }
    public String getInfo(){
        return "角色ID:"+this.rid+" "+"角色名称:"+this.rname;
    }
}
class Group{
    private int gid;
    private String gname;
    private Role [] roles;
    private Action [] actions;
    public Group(int gid,String gname){
        this.gid = gid;
        this.gname = gname;
    }
    public void setActions(Action [] actions){
        this.actions = actions;
    }
    public Action [] getActions(){
        return this.actions;
    }
    public void setRoles(Role [] roles){
        this.roles = roles;
    }
    public Role []  getRoles(){
        return this.roles;
    }
    public String getInfo(){
        return "权限组ID:"+this.gid+" "+"权限组名称:"+this.gname;
    }
}
class Action{
    private int aid;
    private String aname;
    private String url;
    private Group group;
    public Action(int aid,String aname,String url){
        this.aid = aid;
        this.aname = aname;
        this.url = url;
    }
    public void setGoup(Group group){
        this.group = group;
    }
    public Group getGroup(){
        return this.group;
    }
    public String getInfo(){
        return "权限ID:"+this.aid+" "+"权限名称:"+this.aname+" "+"权限路径:"+this.url;
    }
}
public class multiple {
    public static void main(String []args){
        Admin a1 = new Admin(1,"Admin");
        Admin a2 = new Admin(2,"Admin");
        Admin a3 = new Admin(3,"Admin");
        Role r1 = new Role(201,"信息整理员");
        Role r2 = new Role(302,"信息发布员");
        Role r3 = new Role(4,"信息审核");
        Group g1 = new Group(561,"信息管理");
        Group g2 = new Group(661,"用户管理");
        Group g3 = new Group(161,"数据管理");
        Group g4 = new Group(761,"接口管理");
        Group g5 = new Group(461,"备份管理");
        Action a11 = new Action(25,"新闻发布1","-");
        Action a12 = new Action(25,"新闻发布10","-");
        Action a13 = new Action(25,"新闻发布14","-");
        Action a14 = new Action(25,"新闻发布41","-");
        Action a15 = new Action(25,"新闻发布17","-");
        Action a16 = new Action(25,"新闻发布15","-");
        Action a17 = new Action(25,"新闻发布155","-");
        Action a18 = new Action(25,"新闻发布122","-");
        Action a19 = new Action(25,"新闻发布12222","-");
        Action a20 = new Action(25,"新闻发布12422","-");
        Action a21 = new Action(25,"新闻发布1252","-");
        Action a22 = new Action(25,"新闻发布1242","-");
        Action a23 = new Action(25,"新闻发布122222","-");
        //管理员与角色
        a1.setRole(r1);
        a2.setRole(r2);
        a3.setRole(r3);
        r1.setAdmins(new Admin[]{a1,a2});
        r2.setAdmins(new Admin[]{a1,a3});
        r3.setAdmins(new Admin[]{a2,a3});
        //角色和权限组
        r1.setGtoups(new Group[]{g1,g2});
        r2.setGtoups(new Group[]{g3,g4});
        r3.setGtoups(new Group[]{g5,g4});
        g1.setRoles(new Role[]{r1,r2});
        g2.setRoles(new Role[]{r1,r3});
        g3.setRoles(new Role[]{r2,r3});
        g4.setRoles(new Role[]{r2,r3});
        g5.setRoles(new Role[]{r1,r3});
        //权限组和权限
        a11.setGoup(g1);
        a12.setGoup(g3);
        a13.setGoup(g3);
        a14.setGoup(g1);
        a15.setGoup(g2);
        a16.setGoup(g4);
        a17.setGoup(g3);
        a18.setGoup(g5);
        a19.setGoup(g2);
        a20.setGoup(g5);
        a21.setGoup(g3);
        a22.setGoup(g4);
        a23.setGoup(g1);
        g1.setActions(new Action[]{a11,a12,a13});
        g2.setActions(new Action[]{a14,a15,a16});
        g3.setActions(new Action[]{a17,a18,a19});
        g4.setActions(new Action[]{a20,a21,a22});
        g5.setActions(new Action[]{a17,a12,a23});
        //
        System.out.println(a1.getInfo());
        System.out.println("\t|--"+a1.getRole().getInfo());
        for(int x=0;x

你可能感兴趣的:(简单Java类之 一对多、 双向一对多、多对多)