In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
What this article shares with you is about the three elements of object-oriented programming. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it with the editor.
The three elements of object-oriented programming are encapsulation, inheritance and polymorphism. Encapsulation is the abstraction of things into classes, exposing the external interface, hiding the implementation and internal data; inheritance means that all the functions of the existing class can be used; polymorphism is the technology that allows the parent object to be equal to one or more child objects.
The three elements of object-oriented programming are encapsulation, inheritance and polymorphism.
Five basic principles of object-oriented: single responsibility principle, open and closed principle, Liskov replacement principle, dependency inversion principle, interface abstraction principle
Encapsulation
Encapsulation is that things are abstracted into classes, the external interfaces are exposed, and the implementation and internal data are hidden.
Inherit
One of the main functions of the object-oriented programming (OOP) language is inheritance. Inheritance is the ability to use all the functionality of an existing class and extend it without rewriting the original class.
New classes created by inheritance are called "subclasses" or "derived classes".
The inherited class is called "base class", "parent class" or "superclass".
The process of inheritance is from the general to the special.
Inheritance can be implemented through "Inheritance" and "Composition".
In some OOP languages, a subclass can inherit multiple base classes. But in general, a subclass can only have one base class, and multiple inheritance can be achieved through multi-level inheritance.
There are three ways to implement the concept of inheritance: implementation inheritance, interface inheritance and visual inheritance.
Implementation inheritance refers to the ability to use the properties and methods of the base class without additional coding
Interface inheritance refers to using only the names of properties and methods, but subclasses must provide the ability to implement
Visual inheritance refers to the ability of a sub-form (class) to use the appearance of the base form (class) and to implement code.
When considering inheritance, it is important to note that the relationship between the two classes should be a "belong" relationship. For example, Employee is a person, and Manager is a person, so both classes can inherit the Person class. But the Leg class cannot inherit the Person class because the leg is not a person.
Abstract classes define only the general properties and methods that will be created by subclasses. Use the keyword Interface instead of Class when creating abstract classes.
The OO development paradigm is roughly as follows: dividing the object → abstract class → to organize the class into a hierarchical structure (inheritance and synthesis) → uses classes and instances to design and implement.
Polymorphisms
Polymorphisn is a technique that allows you to set a parent object to be equal to one or more of its children. After assignment, the parent object can operate differently according to the properties of the child object currently assigned to it. To put it simply, it is a sentence that allows pointers of subclass types to be assigned to pointers of parent class types.
There are two ways to achieve polymorphism, override and overload.
Override refers to the practice of a subclass redefining the virtual function of the parent class.
Overloading means that multiple functions of the same name are allowed, and these functions have different parameter tables (either the number of parameters, the type of parameters, or both).
In fact, the concept of overloading does not belong to "object-oriented programming". The implementation of overloading is that the compiler modifies the name of the function with the same name according to the different parameter tables of the function. then these functions with the same name become different functions (at least for the compiler).
For example, there are two functions with the same name: function func (p:integer): integer; and function func (p:string): integer;. Then the function name modified by the compiler may look like this: int_func, str_func.
The calls to these two functions are determined between compilers and are static (remember: static). That is, their addresses are bound at compile time (early binding), so overloading has nothing to do with polymorphism! What is really related to polymorphism is "covering".
When the subclass redefines the virtual function of the parent class, the parent pointer is dynamic according to the different subclass pointers assigned to it. Belongs to the function of the subclass, such a function call cannot be determined during compilation (the address of the virtual function of the called subclass cannot be given).
Therefore, such a function address is bound at run time (late bound). The conclusion is that overloading is only a language feature, independent of polymorphism and object-oriented! To quote Bruce Eckel: "Don't be silly, if it's not late-bound, it's not polymorphic."
So what is the role of polymorphism? We know that encapsulation can hide implementation details and make code modular; inheritance can extend existing code modules (classes)
Their purpose is for code reuse. The purpose of polymorphism is to achieve another purpose-interface reuse! The role of polymorphism is to ensure the correct invocation of a property of an instance of any class in the family tree when inheriting and deriving.
These are the three elements of object-oriented programming. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.