请找出下面的代码片段,想知道我的思路是否正确
聚合:两个对象可以独立存在(用户仍然存在)
合成:对象不能独立生存(银行死账户死)
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
}
}
暂无答案!
目前还没有任何答案,快来回答吧!