/** * UML类图-(类/接口)关系 * 六大关系: * 1、依赖(箭头+虚线,箭头指向被依赖类) * 例子:驾驶员类Driver需要依赖汽车类Car才能高速移动 * 类图:Driver-->Car * 代码:依赖类Driver注入被依赖类Car * public class DependenceRelation { * public static void main(String[] args) { * new Driver(new Car()); * } * } * class Driver { * public Driver(Car car){ * System.out.print("小徐"); * car.startup(); * } * } * class Car { * public void startup(){ * System.out.println("高速移动"); * } * } * * 2、关联(一对一/一对多/多对多,箭头+实线,箭头指向关联类) * 例子:员工类Employee一对一关联工卡类WorkCard才能获取信息 * 类图:Employee<——WorkCard * 代码:关联类Employee关联被关联类WorkCard * public class AssociationRelation { * public static void main(String[] args) { * new Employee(new WorkCard()).doSomething(); * } * } * class Employee { * private WorkCard workCard; * public Employee(WorkCard workCard) { * this.workCard = workCard; * } * public void doSomething(){ * System.out.println(workCard.Id+"-"+workCard.name); * } * } * class WorkCard { * protected Integer Id=1; * protected String name="小徐"; * } * * 3、聚合(强的关联关系,箭头+实线+空心菱形,箭头指向个体类) * 例子:书类Book聚合书页类BooPage才能聚成整体 * 类图:Book◇——>BookPage * 代码:整体类Book聚合个体类BookPage * public class AggregationRelation { * public static void main(String[] args) { * List<BookPage> bookPageList = Arrays.asList( * new BookPage("第一页") * , new BookPage("第二页") * , new BookPage("第三页")); * System.out.println(new Book(bookPageList)); * } * } * class Book { * private List<BookPage> bookPageList; * public Book(List<BookPage> bookPageList) { * this.bookPageList = bookPageList; * } * @Override * public String toString() { * return "Book{" + * "bookPageList=" + bookPageList + * '}'; * } * } * class BookPage { * protected String pageName; * public BookPage(String pageName) { * this.pageName = pageName; * } * @Override * public String toString() { * return "BookPage{" + * "pageName='" + pageName + '\'' + * '}'; * } * } * * 4、组合(更强的关联关系,箭头+实线+实心菱形,箭头指向个体类) * 例子:书类Book组合书页类BooPage才能组成整体 * 类图:Book◆——>BookPage * 代码:整体类Book组合个体类BookPage * public class CompositionRelation { * public static void main(String[] args) { * //代码同上聚合 * List<BookPage> bookPageList = Arrays.asList( * new BookPage("第一页") * , new BookPage("第二页") * , new BookPage("第三页")); * System.out.println(new Book(bookPageList)); * } * } * --用例类同上聚合-- * * 5、继承(空心三角形+实线,三角形指向父类) * 例子:父类Parent被继承子类Child才能引用父类属性和方法 * 类图:Parent◁——Child * 代码:父类Parent被继承子类Child * public class InheritanceRelation { * public static void main(String[] args) { * new Child().childSay(); * } * } * class Parent { * public void parentSay(){ * System.out.print("爸爸"); * } * } * class Child extends Parent { * public void childSay(){ * super.parentSay(); * System.out.print("儿子"); * } * } * * 6、实现(空心三角形+虚线,三角形指向接口) * 例子:功能接口Function被实现实现类FunctionImpl才能实现方法 * 类图:Function◁--FunctionImpl * 代码:功能接口Function被实现实现类FunctionImpl * public class RealizationRelation { * public static void main(String[] args) { * new FunctionImpl().say(); * } * } * interface Function { * public void say(); * } * class FunctionImpl implements Function { * @Override * public void say() { * System.out.println("Hello BB-X!"); * } * } */ /** * 其中, * USE-A:表示依赖关系 * HAS-A:表示组合关系 * IS-A :表示继承关系 */
UML类图关系总结
于 2022-08-07 22:20:54 首次发布