Home > Web Front-end > JS Tutorial > body text

Javascript inheritance mechanism example_javascript skills

WBOY
Release: 2016-05-16 18:48:08
Original
797 people have browsed it

Actually, there are only two types of geometric shapes, namely ellipse (which is circular) and polygon (which has a certain number of sides). A circle is a type of ellipse that has only one focus. Triangles, rectangles, and pentagons are all types of polygons that have varying numbers of sides. A square is a type of rectangle with all sides of the same length. This constitutes a perfect inheritance relationship.

In this example, Shape is the base class for Ellipse and Polygon (all classes inherit from it). The ellipse has an attribute foci, which indicates the number of foci the ellipse has. Circle inherits ellipse, so circle is a subclass of ellipse, and ellipse is the superclass of circle. Similarly, Triangle, Rectangle, and Pentagon are all subclasses of Polygon, and Polygon is their superclass. Finally, Square inherits from Rectangle.

This inheritance relationship is best explained using diagrams, which is where UML (Unified Modeling Language) comes in. One of the main uses of UML is to visually represent complex object relationships like inheritance. Figure 4-1 is a UML diagram explaining the relationship between Shape and its subclasses.

In UML, each box represents a class, described by the class name. The line segments at the top of Triangle, Rectangle and Pentagon come together and point to Shape, indicating that these classes are inherited from Shape. Likewise, the arrow pointing from Square to Rectangle illustrates the inheritance relationship between them.

If you are interested in learning UML, you can refer to the "UML User Guide (Second Edition)" written by three UML founders.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template