Six .Python object-oriented

table of Contents

  • Language Category
  • Object-Oriented
  • Object-oriented three elements
  • Python object-oriented

01 Language Category

Machine-oriented:

Abstract into machine instructions, the machine easy to understand. Representative: Assembly Language

Process-oriented:

Do one thing, to exclude steps, the first step to do, to do the second step, if the situation occurs A, What will we do if the situation B appeared, what deal.
Small scale of the problem, may step of the step by step process
on behalf of C language

Object-oriented OPP

Comparison function, object-oriented is larger packages, packaging a plurality of functions according to a method in an object
prior to completion of a demand for a first determined duty - to do (method)
determined under the responsibility of different objects in the object different methods inside the package (s)
to finalize the code is the order to allow different objects of different calling methods

Focus on objects and responsibilities, different objects assume different responsibilities
more suited to respond to complex changes in demand, is designed to deal with complex project development, provides fixed routine
requires a process-oriented basis, and then learn some object-oriented syntax
on behalf of C ++, java, Python, etc.

02 Object-Oriented

  • What is object-oriented?
    An understanding of the world, the world of analysis methodology. The abstract class of all things.

  • Class class
    class is an abstract concept, everything is abstract, is a set of common characteristics of a class of things.

  • The object instance, object
    object is a concrete class, an entity
    for each of us to this individual, different human entities are abstractions.

* Property, which is an abstract state of an object

* Operation, it is an abstract object behavior

03 three elements of object-oriented

  • 1. Packaging
    assembly: the operation data and are assembled together.
    Hidden data: Foreign exposed only some of the interface, the interface accessed through the object.

  • 2. inheritance
    multi-multiplexing, inherited do not have to write a
    multiple inheritance little modification, use inheritance to modify to reflect the personality

  • 3. Polymorphic
    object-oriented programming where most flexible, dynamic binding

04 Python object-oriented

  • Python's class
class ClassName:
    语句块
# 1.必须使用class关键字
# 2.类名必须是用大驼峰命名
# 3.类定义完成后,就产生了一个类对象,绑定到了ClassName上
class MyClass:
    """A example class"""
    x = 'abc' #类属性

    def foo(self): #类属性,也是方法
        print(self.x)
        return  'My Class'

print(MyClass.x)
print(MyClass.foo)
print(MyClass.__doc__)
print(MyClass.__name__)
  • Class object
    class also an object itself, can be called an object class:
    class object classes and their properties
    • 1. Object class, the class definition of a class object is generated
    • 2. The properties of the class, the class definition of class variables and methods defined in the class attributes are
    • 3. class variables, x is a variable class MyClass
  • Examples of methods
__new__()实例创建的方法,一般很少创建,默认隐藏调用
__init__()实例初始化方法
__init__()不能有返回值,有也只是能是None
Python 中一个class中只有一个__init__()
  • Instance of an object instance, Object
    instance variables are each instance its own variables, it is unique of its own; that is, class variables class variables are shared by all instances of the properties and methods of the class
Property name description
name Object Name
class Type of object
dict Dictionary object's attributes
qualname Qualified class name
  • Class variable names are named using all uppercase

  • Find the order of instance attributes:
    refers to an instance using access property, will go first to their own dict, dict will find not find the upper class.
    If the direct use __dict __ [variable name] access variables will not find in accordance with the above order a.

Decorate a class

Examples

def setnameproperty(name):
    def _setnameproperty(cls):
        cls.NAME = name
        return cls  #必须返回类
    return _setnameproperty

@setnameproperty('My Class')
class MyClass:
    pass
print(MyClass.NAME)
print(MyClass.__dict__)

Class methods and static methods

def setnameproperty(name):
    def _setnameproperty(cls):
        cls.NAME = name
        return cls  #返回类
    return _setnameproperty


@setnameproperty('My Class')
class MyClass:
    x = 123
    def __init__(self):
        print('init')
    def foo(self):
        return 'foo'

    @staticmethod
    def bar():
        print('bar')

    @classmethod
    def clsmtd(cls):
        print('{}的x=={}'.format(cls.__name__, cls.x))


# print(MyClass.NAME)
# print(MyClass.__dict__)

MyClass.bar()
print(MyClass.__dict__)
MyClass.clsmtd()
a = MyClass()
a.clsmtd() #隐式为a.__class__.clsmtd() 类方法等价于java中的静态方法

java class methods, static methods is that
static methods in python is generally refers to the function, jurisdiction for the class, but the class can not be attributed to the real meaning.
It may be understood as a method in the class of nominal, less use.

The method of the class, no instances of, as long as the class definition is present, can be called
ClassName.clsmtd ()
ClassName (). Clsmtd ()

Access control

  • Public property (public)

  • Private property (Private):
    two underscores behalf of private, but not really private, in essence, Python interpreter will change its name to class _ __ variable name

  • Protection (Protect)
    an underscore is a convention, known as protection variables.

Destroying objects

Class can be defined __del__ method called destructor (Method)
acting: when the instance of the call class destroyed to release the resources occupied

Since Python implementation of the garbage collection mechanism, this method can not determine the proper execution, if necessary, use the del statement to delete instance, to call this method manually.

Overloaded methods

In other high-level object-oriented language, it has overloaded the concept of
so-called heavy-duty, that is, the same method name, but the number of parameters, not the same type, the same method is overloaded.
But only Python rewrite, does not override!
Python, the process (function) is defined, the parameter is very flexible, do not need to specify the type, even if the specified type only illustrative and not a constraint.
No fixed number of parameters (variable parameter). Definition of a function can achieve many different forms of argument call.
So you do not need to reload Python methods.

Class inheritance

Python3 all classes are the parent class Object

View special properties and methods inherited:

Attributes description
__base__ Base class
__bases__ Base class tuple class
__mro__ The method of displaying the search order, a base class tuple
__subclasses__() Subclass list of classes

Python supports multiple inheritance
Python uses MRO (methon resoultion order) to solve the base class search order
MRO has three search algorithm
1) classical algorithm, from left to right, depth-first strategy
2) upgrade the new class of algorithms, classical algorithm, duplicate reservation only the last
3) C3 algorithm, when the class is created, it is a calculated MRO ordered list. Version 2.3 supports after,
A Python 3 only supported algorithm.

Guess you like

Origin www.cnblogs.com/luckyleaf/p/12112876.html