Python object-oriented (end)

Object-Oriented

First, special modifiers

  Total members:

  Private members: __ member name. (Privatization: plus two underscores before the member name)

    Actually packaged as class name __ _ Member Name

    - can not directly access external

    - subclass can not directly access private members of the parent class  1 class F:

 2     def __init__(self):
 3         self.ge=123
 4         self.__gene=123
 5         
 6 classs S(F):
 7     def __init__(self,name):
 8         self.name=name
 9         self.__age=18
10         super(S,self).__init__()
11     def show(self):
12         print(self.name)
13         print(self.__age)
14         print(self.ge)
15         print(self.__gene)
16 
17 s=S('zhangsan')
18 s.show()

Second, the members of the special

  __init __: obj = class name () to create the class objects, and the default execution __init __ () method;

  __del__: Instead destructor, and constructor called when the object is destroyed;

   __call__: Object name + () default method of execution;

1 class Foo:
2     def  __init__(self):
3         print('init')
4 
5     def  __call__(delf,*args,**kwargs):
6          print('call')
7 
8 Foo()()

  __int __: int (object), automatically performs __int __ () method.

  __str __: str (object), automatically performs __str __ () method.

 1 class  Foo:
 2     def  __init__(self):
 3         pass
 4 
 5     def  __int__(self):
 6         return 1111
 7 
 8     def  __str__(self):
 9         return  'asdf'
10 
11 obj=Foo()
12 print(obj,int(obj),str(obj))

  __dict__: all returned content objects encapsulated by a dictionary;

Three, metaclass, class fathers

  a, Python everything is an object in

  b、class  Foo:

      pass

    obj=Foo()

    obj is an instance of an object Foo; Foo is a type of class object

. 1  class   Foo (Object):
 2      DEF   FUNC (Self):
 . 3          Print (123 )
 . 4 corresponds ==================
 . 5  DEF   function (Self):
 . 6      Print ( 123 )
 . 7  
. 8 Foo = type ( ' Foo ' , (Object,), { ' func ' : function})
 . 9 declare a class object; class object name: Foo, inherited (object), the class object has a method called func
 1 class  MyType(type):
 2     def  __init__(self,*args,**kwargs):
 3         #self=Foo
 4         print('MyType    init')
 5         super(Mytype,self).__init(what,bases,dict)
 6 
 7     def  __call__(self,*args,**kwargs):
 8         #self=Foo
 9         print('MyType    call')
10         obj=self.__new__(selt,*args,**kwargs)
11         self.__init__(obj)
12 
13 class  Foo(object,metaclass=MyType):
14     def  __init__(self)
15         print('Foo    init')
16 
17     def  __new__(cls,*args,**kwargs):
18         #cls=Foo
19         print('Foo   __new__')
20         return  object.__new__(cls,*args,**kwargs)
21 
22 coj=Foo()
23 
24 Output:
 25          MyType the init
 26 is          MyType Call
 27          Foo    __new__ is 
28          Foo the init
 29  
30  first stage: execution of the code from top to bottom interpreter class Foo created, the first call MyType __ the __init ();
 31 is  a second phase: Foo class creates object obj;
 32      . 1 , Foo is MyType objects, so Foo () first calls MyType in the __call __ ();
 33 is      2 , creating the object obj by class Foo: turn call
 34 is              MyType of the __call __ (): Object + () method is called automatically
 35              of Foo __new __ (): Foo created object
 36              of Foo __init __ (): Foo object instantiated

  c, an object class object type is type (...); object instance objects are class objects

Guess you like

Origin www.cnblogs.com/TianLiang-2000/p/11616312.html