How to translate UML relationships into code

Source: Internet
Author: User
Tags inheritance

1. Inheritance relationship (generalization relationship)

Description: The inheritance relationship is a subclass (derived class) that inherits the parent class (the base class), or the child interface inherits the parent interface's relationship. The subclass object "is a" parent object, such as a bird is an animal.

"UML Diagram":

Diagram: Animal is the parent class, Bird class, Fish class, dog class inherit the animal class, they not only inherit animal common method breath (), but also expand the relevant methods according to their actual needs (Fly () method, swim () method, Run ( ) method).

"Corresponding Code":

Animal Class (Parent Class): Class    
   Animal    
   {public    
    void breath () {}    
   }    
//bird class, Fish class, and dog Class (subclass): Class    
   Bird: Animal    
  {public    
    void Fly () {}    
  }    
  class Fish:animal    
  {public    
    void swim () {}    
  }    
  clas S dog:animal    
  {public    
    void Run ()}    
  }

"The Finishing Touches":

★ The subclass can inherit all the private properties and methods of the parent class, and can expand according to the actual situation (add the attribute or method), as in the above example, the fly () method is the development of the animal species;

★ Class can only have single inheritance (a subclass can inherit only a parent class, a parent class can inherit from multiple subclasses), and an interface can have multiple inheritance (a sub-interface can inherit multiple parent interfaces).

2, the realization of relations

"description": Implementing a relationship is the relationship between the class implementation interface. The following is illustrated by the example in the "Liar design pattern".

"UML Diagram":

Graphic: Robot cat, Monkey King, Pig Eight commandments can be changed things, but because other animals do not have the same abilities as the three of them, it is not possible to add this method directly to the parent animal, but to abstract an interface called "Changing things". Then let the animals with this ability (such as Machine Cat, Monkey King, Pig Eight precepts) directly to implement this interface.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.