Python also supports object-oriented programming. This section focuses on some of the basics of Python object-oriented programming.
What is object-oriented programming?
1. Object-oriented programming is a programming paradigm
2. Think of the program as a mutual invocation of different objects
3. Establishing an object model for the real world
The basic idea of object-oriented programming:
1. Class used to define abstract types
2. Instances are created based on the definition of the class
How do I define a class and create an instance of it?
class Animal (object): pass>>> dog = Animal ()>>> cat = Animal ()
What is an instance property
The Dog,cat instance is created above. How do you get these instances to have their own different properties? If your dog has a name, or cat has a name?
class Animal (object): pass>>> dog = Animal ()'wangcai'>>> cat = Animal () 'beibei'
Because Python is a dynamic language, you can add property name directly to an instance.
Sometimes we want Dog/cat to have the property name when she is born. How do you do it?
class Animal (object): def __init__ (self,name): = name >>> dog = Animal ('wangcai')print Dog.namewangcai
When an instance is created, the __init__ () method is called automatically, and the first parameter, self, is passed into the instance's reference by the Python interpreter.
We sometimes bind a lot of properties to an instance, but we don't want some properties to be accessed directly from outside.
Access restrictions
Python's control over property permissions is implemented by property names, which cannot be directly accessed externally if a double underscore starts (__). Similar to (private)
>>>classAnimal (object):def __init__(Self,name,birth): Self.name=name self.__birth=Birth>>> dog = Animal ('Wangcai','2016-08-04')>>>PrintDog.namewangcai>>>PrintDog.__birthTraceback (most recent): File"<pyshell#100>", Line 1,inch<module>PrintDog.__birthAttributeerror:'Animal'object has no attribute'__birth'
The attribute ' _xxx ', which begins with a single underscore, can be accessed by the derived class subclass. Type in (protected).
Now that the instance has attributes, is it possible to define the properties of the class?
Class properties
What is the difference between a class attribute and an instance property?
Instance properties Each instance owns and is independent of each other, and the class attribute has only one copy.
class Person (object): ' West ' def __init__ (self,name): =name print person.addresswest>>> p1 = person (' Liunx')print p1.addresswest
The Class property has only one copy, so the class property accessed by the instance changes when the person class attribute address changes.
' Earth '>>> p1.address'earth'
But what if the class attribute and the instance property name conflict?
>>>classPerson (object): Address='West' def __init__(self,name): Self.name=name>>> P1 = Person ('Liunx')>>> p1.address ='Earth'//Instance Properties Address>>>Printperson.addresswest>>> person.address ='west_1'//Modifying class Properties>>>PrintP1.address//instance property is not changed, or Earchearth
It can be seen that when instance properties and class attributes have the same name, the instance property has a high precedence, which masks access to the class properties .
The class properties of Python can be likened to static variables in Java.
Sometimes we do not want the private properties of the instance to be directly displayed by external calls. But you want to be implicitly called externally.
Defining instance methods
class Animal (object): def __init__ (self,name): Self . __name = name def get_name (self): return to self. __name >>> dog = Animal ('wangcai')Print dog.get_name () Wangcai
This is the form of Python object-oriented data encapsulation. Protect the consistency of internal data.
Since the instance has its own method, the class also has a class method.
Defining class methods
>>>classPerson (object): Count=0 @classmethoddefHow_many (CLS):returnCls.countdef __init__(self, name): Self.name=name Person.count= Person.count + 1 >>>Printperson.how_many (0)>>> P1 = Person ('Liunx')>>>PrintPerson.how_many ()1
By marking a @classmethod, you can bind a method to a person class, rather than an instance of a class. The first parameter of a class method is passed in to the class itself. The above cls.count is equivalent to the Person.count. Class method cannot get any instance variables and intelligently obtains a reference to the class. A static method that can be analogous to Java.
Summary: Mainly to explain some object-oriented foundation.
Python Learning day 14th-Object Oriented Programming basics