You all know the classes in Java, but do you know the relationship between classes?

1. Association

1. One-way association: Members of another class can be accessed from one class, and there is a reference to this class.
2. Two-way association: Two classes can visit each other and have references to each other.
3. Self-association: self or this is called in this class.
4. Multi-dimensional association: multiple classes have one-way associations and two-way associations with each other, and can also have self-association and multiple-dimensional associations.
5. One-to-many association: A class has multiple class references.
6. Many-to-many association: There are one-way associations and two-way associations between multiple classes.
7. When an attribute of one class holds a reference to an instance of another class, an association occurs.

Second, the aggregation relationship

1. Aggregation association is a special association. It means that the relationship between classes is the relationship between the whole and the part. In short: a class in an association relationship describes a larger thing, which is composed of smaller things.
2. In the aggregation relationship, a large category is composed of multiple small categories, but without this large category, these small categories can be aggregated into another category and continue to be used, which is the difference from the combination relationship.
3. The aggregation relationship is a kind of association relationship, which is a strong association relationship.
4. Aggregation is the relationship between the whole and the parts. For example, a car is composed of engines, tires and other parts.
5. The aggregation relationship is also realized through member variables. However, the two classes involved in the association relationship are at the same level, while in the aggregation relationship, the two classes are at different levels, one representing the whole and the other representing the part.
6. In layman's terms: A "car" has an engine and four tires. If this "car" is destroyed, its engine and tires can be mounted on other cars.

Three, generalization relationship

1. In UML, the generalization relationship is used to express the inheritance relationship between class and class, interface and interface. To be more precise, there is not only an inheritance relationship, but also a realization relationship between classes and interfaces.
2. The generalization relationship includes inheritance relationship and realization relationship.

Generalization is to abstract a parent class from a subclass, including inheritance relationships (a subclass is concretized by the parent class).
Inheritance relationship:

            <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n430" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;">
public class Father {
    
    }
            public class Son extends Father{
    
    
             //继承Father类
            }</pre>

Inheritance refers to a class (called subclass, subinterface) inheriting the functions of another class (called parent class, parent interface). In UML class diagram design, inheritance uses a solid line with a hollow triangle arrow Indicates that the subclass points to the parent class, or the subinterface points to the parent interface.
Insert picture description here
Inheritance and generalization can be seen as a reverse process to
achieve the relationship:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n440" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;">
public interface A {
    
    
         //    声明一个接口(注意:接口不是类)
        }
        public class B implements A{
    
    
         //    实现接口A
        }</pre>

Implementation refers to the function that a class implements an interface interface (can be multiple). In the UML class diagram design, the implementation is represented by a dashed line with a hollow triangle arrow, pointing from the class to the implemented interface.
Insert picture description here

Four, combination relationship

Combination: Combination relationship means that two are indispensable, and both exist and disappear at the same time. Often use construction methods to achieve the purpose of initialization,

        <pre spellcheck="false" 
class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n342" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;">
public Driver(Car car){
    
       //  构造方法
         mycar = car; 
        }</pre>

The whole part has the life cycle of the part, so when the whole part is deleted, the part will be deleted. Moreover, multiple whole parts cannot share the same part at the same time.

In the combination relationship, a large category is composed of multiple small categories. Without this large category, the small category cannot exist.

The aggregation relationship is when describing a big thing, the big thing can include small things or not small things, such as libraries and books, and the combination is that the existence of a big thing must be composed of multiple small things. Composed of transactions, small transactions are not allowed by default. For example, humans and organs, human organs can be transplanted, this is a polymerization relationship; humans and limbs, without a human, the limbs are useless, this is a combined relationship.

The combination relationship is a kind of association relationship, which is stronger than the aggregation relationship.

It requires that the object representing the whole in the ordinary aggregation relationship is responsible for the life cycle of the object representing the part. In layman's terms: "human" has four "members". If "human" dies, then the four members are useless.

Five, dependency

 依赖 : A 类 方法 中 的参数 包 含 了B 。
 < pre spellcheck = "false" class = "md-fences md-end-block ty-contain-cm modeLoaded" lang = "" cid = "n333" mdtype = "fences" style = "box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;" >
																       public class Car {
	 public static void run()
	 {
		 System.err.println( "汽车在跑" );
	 }
 }
 public class Driver {
	 public void Driver( Car car ) /* 使用传递形参形成依赖 */
	 {
		 car.run();
	 }


	 public void Driver()
	 {
		 /* 使用局部变量形成依赖 */
		 Car car = new Car();
		 car.run();
	 }


	 public void Driver1()
	 {
		 Car.run(); /* 使用静态方法形成依赖 */
	 }
 }
 < / pre>

For two relatively independent systems, when one system is responsible for constructing an instance of the other system, or depends on the services of the other system, the two systems have a dependency relationship.

A class will use another class at a certain moment, then these two classes are dependencies. It is a short-lived relationship.

Dependency is also the connection between classes

Dependence is always one-way.

Dependency is reflected in the code as local variables, method parameters, or calls to static methods.

For example, people rely on the relationship of cars when they travel.

Six, summary

The close relationship between class and class relationship ranks the behavior: Implementation> Aggregation> Association> Dependence. I believe you have already understood the relationship between classes in Java.

Novice to learn Java technology, I recommend joining my Java novice learning corner , and ask any technical questions at any time.

Guess you like

Origin blog.csdn.net/weixin_49794051/article/details/111509101