通过简单例子 | 快速理清 UML类图中六大关系
類與類之間的六大關(guān)系
前言:
最近學(xué)校在上統(tǒng)一建模語言 UML ,也是畢業(yè)設(shè)計中需要用到。我感覺學(xué)會 UML 圖對編程的作用是很大的,將需要做的東西抽象出來,作圖,然后再進(jìn)行編碼,我覺得思路會比較清晰。所以打算把這個方面也好好掌握掌握。
希望這篇文章能夠給大家?guī)硇┰S收獲,讓大家趁興而歸。
一、單個類的類圖
一步一步來,我們先學(xué)學(xué)如何使用 UML 圖來表示單個類。
我先把類貼下面:
package uml;/*** @Author: crush* @Date: 2021-09-30 15:00* version 1.0*/ public class Person {private String name;private Integer age;private static String school="某小學(xué)";public static String nationality="中國";public Person() {}public Person(String name, Integer age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public static String getSchool() {return school;}public static void setSchool(String school) {Person.school = school;}public static String getNationality() {return nationality;}public static void setNationality(String nationality) {Person.nationality = nationality;}public void selfIntroduction(String name, Integer age, String school){System.out.println("做一個自我介紹,我的名字是:"+name+",今年"+age+"歲了,來自于"+school);} }這個類還是非常簡單的哈,接下來就是要如何用一個類圖來進(jìn)行描述呢?
如下圖:
解釋:
上半部分是 Person 類的屬性,下半部分是 Person 類的方法
- name:String 描述的是:private String name;
-號:表示為私有屬性( private ),反過來 + :就表示 public
name:為屬性名稱
:xxx :是表示屬性的類型的。此處為 String 類型
-School:String="某幼兒園" :描述的是 private static String school="某小學(xué)";
下劃線是表示此屬性為 static(靜態(tài)屬性)
“某幼兒園” 表示有默認(rèn)值。 其他同上。
+ getNationality():String 描述的是
public static void setNationality(String nationality) {Person.nationality = nationality; }和上面基本一樣,+ 表示 public ,下劃線表示 static 修飾,getNationality() 表示方法名,String 表示返回值為String類型。
但是平時中,我們通常都是多個類之間有關(guān)系,不是個孤零零的孤寡老人。
二、多個類之間的關(guān)系
表達(dá)多個類之間的關(guān)系有以下六種:
三、繼承和實(shí)現(xiàn)的類圖
3.1、繼承
【泛化關(guān)系】:是一種繼承關(guān)系,表示一般與特殊的關(guān)系,它指定了子類如何特化父類的所有特征和行為。例如:老虎是動物的一種,即有老虎的特性也有動物的共性
1)代碼
動物類:
public class Animal {private String name;private Integer age;public Animal() {}public Animal(String name, Integer age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;} }貓類繼承動物類:
public class Cat extends Animal {private String breeds;public Cat(String name, Integer age, String breeds) {super(name, age);this.breeds = breeds;}public String getBreeds() {return breeds;}public void setBreeds(String breeds) {this.breeds = breeds;}public void selfIntroduction(String name,Integer age,String breeds){System.out.println("我叫"+name+",是一只"+breeds+"品種的貓,今年"+age+"歲了,");} }我們用類圖來表示這個關(guān)系。
4)圖示
箭頭要用對,不然關(guān)系就完全不一樣拉。
3.2、實(shí)現(xiàn)
【實(shí)現(xiàn)關(guān)系】:是一種類與接口的關(guān)系,表示類是接口所有特征和行為的實(shí)現(xiàn).
1) 代碼
吃睡接口,我們再讓動物類來實(shí)現(xiàn)他兩。
public interface Eat {void eat(); } public interface Sleep {void sleep(); } public class Animal implements Eat,Sleep{private String name;private Integer age;public Animal() {}public Animal(String name, Integer age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}@Overridepublic void eat() {System.out.println("吃東西");}@Overridepublic void sleep() {System.out.println("睡覺");} }2) 圖示
四、關(guān)聯(lián)關(guān)系的類圖
【關(guān)聯(lián)關(guān)系】:是一種擁有的關(guān)系,它使一個類知道另一個類的屬性和方法;如:老師與學(xué)生,丈夫與妻子關(guān)聯(lián)可以是雙向的,也可以是單向的。雙向的關(guān)聯(lián)可以有兩個箭頭或者沒有箭頭,單向的關(guān)聯(lián)有一個箭頭。
我們增添一個出身地的類,每個動物都會有一個出生地的地方。
我們將這個出生地和動物關(guān)聯(lián)起來。
4.1、代碼
/*** @Author: crush* @Date: 2021-09-30 19:11* version 1.0* 出生地*/ public class Birthplace {private String birthplace;public Birthplace(String birthplace) {this.birthplace = birthplace;}public String getBirthplace() {return birthplace;}public void setBirthplace(String birthplace) {this.birthplace = birthplace;} }與動物類關(guān)聯(lián)起來:
public class Animal implements Eat,Sleep{private String name;private Integer age;private Birthplace birthplace;public Animal() {}public Animal(String name, Integer age, Birthplace birthplace) {this.name = name;this.age = age;this.birthplace = birthplace;}public Birthplace getBirthplace() {return birthplace;}public void setBirthplace(Birthplace birthplace) {this.birthplace = birthplace;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}@Overridepublic void eat() {System.out.println("吃東西");}@Overridepublic void sleep() {System.out.println("睡覺");} }在自我介紹方法中增添輸出。
public class Cat extends Animal {private String breeds;public Cat(String name, Integer age,Birthplace birthplace, String breeds) {super(name, age,birthplace);this.breeds = breeds;}public String getBreeds() {return breeds;}public void setBreeds(String breeds) {this.breeds = breeds;}public void selfIntroduction(String name,Integer age,String breeds,Birthplace birthplace){System.out.println("我叫"+name+",是一只"+breeds+"品種的貓,今年"+age+"歲了,出生于"+birthplace.getBirthplace());} }4.2、圖示
五、聚合和組合關(guān)系的類圖
5.1、聚合
聚合 ( Aggregation ) : 是整體與部分的關(guān)系,且部分可以離開整體而單獨(dú)存在。如車和輪胎是整體和部分的關(guān)系,輪胎離開車仍然可以存在。
聚合關(guān)系是關(guān)聯(lián)關(guān)系的一種,是強(qiáng)的關(guān)聯(lián)關(guān)系;關(guān)聯(lián)和聚合在語法上無法區(qū)分,必須考察具體的邏輯關(guān)系。
先說說我這個例子,我們再寫代碼。
【例子】每臺車都會有四個輪胎和一個引擎,輪胎離開車可以單獨(dú)存在的,引擎同樣也是。
1)代碼
public class Wheel {private String type;public Wheel(String type) {this.type = type;}public String getType() {return type;}public void setType(String type) {this.type = type;}public void move(){System.out.println("滾動!!!");} } public class Engine {private String type;public Engine(String type) {this.type = type;}public String getType() {return type;}public void setType(String type) {this.type = type;}public void start(){System.out.println("啟動引擎!!!");} } public class Car {private Wheel wheel;private Engine engine;public Car(Wheel wheel, Engine engine) {this.wheel = wheel;this.engine = engine;}public Wheel getWheel() {return wheel;}public void setWheel(Wheel wheel) {this.wheel = wheel;}public Engine getEngine() {return engine;}public void setEngine(Engine engine) {this.engine = engine;}public void go(){System.out.println("開車出去浪;啊");} }用類圖如何表示勒,接著往下看👇
2)圖示
5.2、組合
組合 ( Composition ) : 是整體與部分的關(guān)系,但部分不能離開整體而單獨(dú)存在。如公司和部門是整體和部分的關(guān)系,沒有公司就不存在部門。還有如人由頭和身體組成,沒有了人,頭和身體還咋存在勒。
組合關(guān)系是關(guān)聯(lián)關(guān)系的一種,是比聚合關(guān)系還要強(qiáng)的關(guān)系,它要求普通的聚合關(guān)系中代表整體的對象負(fù)責(zé)代表部分的對象的生命周期。
1)代碼
public class Body {private double size;public Body(double size) {this.size = size;}public double getSize() {return size;}public void setSize(double size) {this.size = size;} } public class Body {private double size;public Body(double size) {this.size = size;}public double getSize() {return size;}public void setSize(double size) {this.size = size;} } public class Person2 {private String name;private Head head;private Body body;public Person2(String name, Head head, Body body) {this.name = name;this.head = head;this.body = body;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Head getHead() {return head;}public void setHead(Head head) {this.head = head;}public Body getBody() {return body;}public void setBody(Body body) {this.body = body;}public void say(){System.out.println("我會說話");} }2)圖示
六、依賴關(guān)系的類圖
依賴(Dependency) 關(guān)系是一種使用關(guān)系,它是對象之間耦合度最弱的一種關(guān)聯(lián)方式,是臨時性的關(guān)聯(lián)。在代碼中,某個類的方法通過局部變量、方法的參數(shù)或者對靜態(tài)方法的調(diào)用來訪問另一個類(被依賴類)中的某些方法來完成一些職責(zé)。
在 UML 類圖中,依賴關(guān)系使用帶箭頭的虛線來表示,箭頭從使用類指向被依賴的類。如人與手機(jī)的關(guān)系圖,人通過手機(jī)的語音傳送方法打電話。
1、代碼
public class Phone {public void callUp(){System.out.println("與人通話");} } public class Person3 {private String name;public Person3(String name) {this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}public void callUp(Phone phone){System.out.println("使用手機(jī)打電話");phone.callUp();} // 下面這種方式也是依賴關(guān)系 // public void callUp(){ // Phone phone=new Phone(); // System.out.println("使用手機(jī)打電話"); // phone.callUp(); // } }2、圖示
七、類關(guān)系的強(qiáng)弱
強(qiáng)弱關(guān)系:泛化 = 實(shí)現(xiàn) > 組合 > 聚合 > 關(guān)聯(lián) > 依賴
另外我們常常說的降低耦合性,也是降低類與類之間的關(guān)系。
八、自言自語
今天的文章就到這里了。
你好,我是博主寧在春:主頁
如若在文章中遇到疑惑,請留言或私信,或者加主頁聯(lián)系方式,都會盡快回復(fù)。
如若發(fā)現(xiàn)文章中存在問題,望你能夠指正,不勝感謝。
如果覺得對你有所幫助的話,請點(diǎn)個贊再走吧!
總結(jié)
以上是生活随笔為你收集整理的通过简单例子 | 快速理清 UML类图中六大关系的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot 实现固定、动态定时
- 下一篇: Java设计模式-建造者模式 理论代码相