Python Object-oriented OOP

Source: Internet
Author: User
Tags instance method

1 Oop object Worldview-oop is the language of human cognition closest to real life
    • All Business Objects
    • The object has the law of motion and the internal state
    • objects can be called to each other
2. Object-oriented
    • Uniqueness: The object is unique and there are no two identical objects unless they are the same object
    • Can be categorized: objects can be categorized
3.oop classification
    • Packaging
    • Inherited
    • Polymorphic
Specific classes:

Structure

Classs Class Name:

Class Body

    • Create an object using the class name (the __INIT__ function has a parameter list other than the first argument)
    • When the object is created, the function is actually executed __init__ , and __init__ does not create the object, but initializes the object
    • When using an object to invoke a method, the first parameter is automatically passed in (self is the default, and the four-letter of this can be replaced by any character, but the position must be the first one)
Scope
    • The immediate subordinate scope of a class is called a class variable
    • Variables associated to an instance are called instance variables

Example:

1 class E: 2     ' E ' # the immediate subordinate scope of a class is called a class variable 3     4     def __init__ (self, name): 5         Self.name = name  #  the variable associated to the instance is called an instance variable
    • Class variables are visible to classes and instances
    • All instances share class variables
    • Python can dynamically add and subtract attributes to an object, and when assigning a value to an instance's class variable, it is equivalent to dynamically adding an attribute to the instance, overriding the class variable

The lookup order of the properties
    • __dict__  :当通过对象动态增加属性的时候,如果存在此属性,会覆盖原属性,不存在会存放到对象.__dict__中。

    • __class__ : 如果通过类名.类属性方式给类属性重新赋值,原类属性会被覆盖

---------------------------------------------------need only objects. __dict__ To see how the content can be distinguished-----------------------------------------------------------------------------

Class adorners are typically used to add properties to a class--Methods are class-level

class method/static method

Methods are defined at the class level, but some methods use instance invocation, and some use classes to invoke

classI:def Print(self):Print('instance Method') @classmethod#when a method is Classmethod decorated, the first parameter becomes the class itself, and such a method is called a class method    defClass_print (CLS):Print(ID (CLS))Print('class Method') @staticmethod#when a method is Staticmethod decorated, the first parameter is not passed automatically, and this method is called a static method .    defstatic_print ():Print('static Method')            defxxx_print ():Print('This is a function')

Instance methods can only be called by an instance

Class method: When a class method can be used by an instance and is used by an instance, the first parameter passed in is the class access control

 

classDoor:def __init__(self, Number, status): Self.number=Number self .__status= Status#double underline start, non-double bottom line ends are private, inaccessible outside the class        defOpen (self):#MethodSelf.__status='opening'            defClose (self): self.__status='closed'        defstatus (self):returnSelf.__status        def __set_number(self, number):#Double Slide First, non-double underline End method is also a private methodSelf.number = number

All double underscores start with a non-double-underlined member, all private members

Private members cannot be accessed outside the class

Python's private members are renamed by name, and the class name + attributes with double-down route are identified in the __dict__ (there is no real private member in Python, but this is negligible)

A variable that is labeled with a single underscore in some code can be interpreted as an internal variable, and you do not want to continue calling outside (in fact it can be called externally), and the interpreter does not do any processing

Starting with double underlines, the method that ends with a non-double underline is a private method.

If you use a @property decorator will turn a function with only the self parameter into a property, the value of the property, the return value of the method

 class   Door:  def  __init__   __status  = status #   Double underline, non-double down line ends are private, cannot be accessed outside the class   @property  #      def   status (self):  return  self. __status  
D = Door ('opening')print(d.status) status is Door in the status method
Inheritance of Classes
  • In parentheses after the class name is the inherited list, called the parent class or the base class or superclass-Python does not recommend multiple inheritance
  • An obvious benefit of inheritance is the ability to get the properties and methods of the parent class, which can be confusing in deep succession.
  • All the public can inherit
  • Nothing private can inherit.
  • What was it, inherited it or something?
  • When a child class and a parent class have a member of the same name, a member of the subclass overrides the member of the same name in the parent class
  • Super (parent, self). Print () # Proxy method for the parent class of the type, and use the obj binding first parameter to specify the immediate parent class to call, and the second parameter specifies what to pass as the first parameter of the method when called

  • When the parent class contains an initialization method with parameters, the subclass must require an initialization method, and the initialization method of the parent class is called in the initialization method
     class   Base:  def  __init__   (self, A, b): Self.  __a  = a self.  __b  = b  def< /span> sum (self):  return  self.__a  + self. __b  
    class Sub (Base):     def __init__ (self, A, B, c):         = c#         self.__a = a#         self.__b = b        super ().  __init__(A, B)
  • Super objects can only get properties of a class

Python Object-oriented OOP

Related Article

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.