Object-Oriented programming language features: encapsulation, inheritance, polymorphism

Source: Internet
Author: User

Object-oriented is a very important programming idea in programming, mastering this thought can improve our development efficiency to a great extent, the following is the three main features of object-oriented and how to use it.

Object-Oriented programming languages: C + +, Python, Java, Swilt, C #

Encapsulation: Hide the implementation details of the object so that the user does not care about these details
The package contains two special encapsulation methods, private properties and private methods, both of which have the same characteristics.
That is, a private property or method can only be called by a method in the class, and cannot call the defined private property outside of the class otherwise it will execute an error.


#示例
Class Home:
def __init__ (self,m):
Self.__m = m

def __one (self):
Prit (' This is Private method ')

def tow (self):
Print (self.__m)
Self.__one ()


H=home (' This is a private attribute ')
H.two ()


Single inheritance: Not change the original class based on both the original class and can add new methods, can be used as an extension of the function, the proposed use of single inheritance in programming,

It is not recommended to use multiple inheritance, multiple inheritance has some flaws in programming, it is easy to have identifier (namespace) conflict problem during program execution, so use multiple inheritance sparingly.

#示例
Class A:
def Aname (self):
Print (' Jimmy ')
Class B (A):
def bname (self):
Print (' Jack ')

p = B ()
P.aname () #用B对象调用A类中的name方法Jimmy
P.bname () #调用B类中的name方法Jack

Polymorphic: A variety of states, refers to the inheritance, derivation of the relationship class, call the base class method, the actual ability to call the subclass of the coverage method is called polymorphism.
Polymorphic invocation methods are related to objects and are not class-related (only dynamic in Python) dynamic calls to multiple homogeneous objects are planned as one class for uniform processing,
Again, the common point is that different objects call the same method to perform different behaviors called polymorphism.

#示例
Class A:
def run (self):
Print (' Class A ' Run method ')

Class B (A):
def run (self):
Print (' Class B ' Run method ')

Class C (B):
def run (self):
Print (' Class C ' Run method ')

def my_run (s):
S.run ()


S1 = A ()
S2 = B ()
My_run (S1)
My_run (S2)

Object-Oriented programming language features: encapsulation, inheritance, polymorphism

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.