1024programmer Java Javascript inheritance mechanism example_javascript skills

Javascript inheritance mechanism example_javascript skills

In fact, there are only two types of geometric shapes, ellipses (which are circular) and polygons (which have 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 with 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.


It is best to use a diagram to explain this inheritance relationship, which is UML ( This is where the 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 books written by the three founders of UML “UML User Guide (Second Edition)”.


This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/722291

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索