java—在同一组类上进行聚合和组合

brc7rcf0  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(158)

请找出下面的代码片段,想知道我的思路是否正确
聚合:两个对象可以独立存在(用户仍然存在)
合成:对象不能独立生存(银行死账户死)

class Bank
{
    Account account;
    List<User> userList;
    public Bank(List userlist) 
    {
            this.account = new Account();   //composition 
            this.userList = userlist;      
    }
}
class Account{
}
class User{
}
public class AssociationAndComposition {
    public static void main(String[] args) {
        User u1 = new User();
        User u2 = new User();
        User u3 = new User();
        List<User> listOfUsers = new ArrayList<>();
        listOfUsers.add(u1);listOfUsers.add(u2);listOfUsers.add(u3);    
        Bank b = new Bank(listOfUsers); //aggregation as users can be independent of bank
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题