简体   繁体   English

类图中的聚合和组合关系

[英]Aggregation and Compostion relationship in class diagram

i want to know that when there is an aggregation or composition relation between the two classes,can both classes share attributes of each other? 我想知道,当两个类之间存在聚合或组合关系时,两个类可以共享彼此的属性吗?

if there is a class called account which has a composition relation with class customer so is class account can access the variables of class customer? 如果有一个名为account的类,它与类客户具有组成关系,那么类帐户可以访问类客户的变量吗?

Yes. 是。 Class can access the non-private fields of related classes, may it be aggregation or composition. 类可以访问相关类的非私有字段,可以是聚合的也可以是合成的。

In the below example, class A & B holds a composition relationship and A can access fields and methods of B. But this depends on the visibility of the variable and method. 在下面的示例中,类A和B拥有组成关系,并且A可以访问B的字段和方法。但这取决于变量和方法的可见性。

class A {
String test;
B b;
void printB(){
b.getTest();}
}

class B {
private String test;
public String getTest(){
return test;
}
}

It is not importent if two classes are connected with relationship or not. 如果两个类之间是否存在关联,则不重要。 Attributes are accessible to any element if visibility of attribute is set to public (or package if elemenents reside in the same package). 如果将属性的可见性设置为public(如果元素位于同一包中,则为包),任何元素都可以访问属性。 For example you can access public attribute of class in activity action which pin type is class with public attributes etc. 例如,您可以在活动操作中访问类别的公共属性,其引脚类型为具有公共属性的类别等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM