Python Object-oriented encapsulation

Source: Internet
Author: User

Generalized encapsulation: Instantiates an object and encapsulates some properties for the object space. Narrowly encapsulated: Private Private members: Privately owned static fields, private methods, private object properties for private static fields, private methods, private object properties, outside of the class cannot be accessed.----#instantiated objects cannot access private static fields, private methods, private object Properties----#class name cannot access private static fields, private methods, private object Propertiesfor private static fields, private methods, private object properties, the inside of the class can be accessed. For private static fields, private methods, private object properties, can only be accessed internally within this class, outside of the class, Derived classes are inaccessible. Hides the property (set to private) in Python, starting with a double underscore----#Actually, this is just a warp operation .----#names that begin with all underscores in a class, such as __x, automatically become: _ Class name __x form----features of this automatic deformation:--------1. The __x defined in the class can only be used internally, such as self.__x, the reference is the deformation to get the result. --------2This variant is actually for external deformation, which cannot be accessed externally by the name __x.--------3__x does not overwrite the __x defined by the parent class when the subclass is defined, because the subclass is formed: The subclass name __x, and the parent class becomes: _ The parent class name __x The property that begins with the double underscore is not overridden by the subclass when it inherits to the subclass. This type of deformation requires attention to the following issues:------------1This mechanism also does not really limit our direct access to properties from the outside, knowing the class name and property name can spell the name: _ Class Name __ Property name------------2The process of morphing occurs only once in the definition of a class, after the defined copy operation, does not deform. ------------3in inheritance, fathers can define methods as private if they do not allow subclasses to overwrite their own methods. Encapsulation is not a mere sense of concealment1. Encapsulating Data----hiding data is not an end in itself. Hide it and then provide an interface to manipulate the data externally, and then we can complete the rigor of data property operations by restricting the operation of the data on the interface attachment. 2. Encapsulation Method----witnessing is the isolation of complexity. Hint: In a programming language, an externally provided interface (an interface can be understood as a portal) can be a function, called an interface function, which is not the same as the concept of an interface, which represents a set of interface functions. 

Python Object-oriented encapsulation

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.