python-- Object Oriented (a)

"""""
First, the definition and the implementation of the class class method
class class name:
def method name (self, arg):
    print(arg)
    return1

Middleman (that is, a custom variable objects only) = class name ()
ret = middleman. method name (1)
print (right)

elf refers to the middleman
self must be added

There are three characteristics of object-oriented
1. Blocking
2. Inheritance
3. Polymorphic: python native polymorphism
"""
#例子:
class Bar:
    def foo(self,arg):
        print(self,arg)

z1=Bar()
print(z1)
z1.foo(111)
print('=============')
z2=Bar()
print(z2)
z2.foo(666)
#结果为:
# #<__main__.Bar object at 0x000001F048FF77F0>
# <__main__.Bar object at 0x000001F048FF77F0> 111
# =============
# <__main__.Bar object at 0x000001F048FF7B70>
# <__main__.Bar object at 0x000001F048FF7B70> 666

class Bar:
     DEF foo (self, Arg):
         Print (self, self.name, Arg)
         # where self is to extract z z is the name that is self intermediate variables or that bring in the number of own 

z = Bar ()
z.name='lin'
z.age=20
z.gender='male'
z.foo(666)
#结果为:
#<__main__.Bar object at 0x000002360E0B7BA8> alex 666
"""
Second, the construction method
"""

class Bar:
    def __init__(self,name,age):
        """
        __initt__ Constructors
        """
        print('123')
    def foo(self):
        print('456')

Z = Bar ( ' LIN ' , 20 is )
 # which can pass into the 
# Usually these things are placed in the init. Also carried out an initial 
# When building a z = Bar () is performed automatically after the internal init method 
# Python automatically help you call the 
Print (z)
z.foo()
"""
The results are:
123
<__main__.Bar object at 0x000001EC423A7E48>
456
"""

# Third, the application 
class the Person:
     DEF  __init__ (Self, name, Age):
        self.name=name
        self.age=age
    def show(self):
        print('%s-%s'%(self.name,self.age))

lin=Person('Lin',20)
lin.show()
ZZZI=Person('zzzi',20)
ZZZI.show()
"""""
Lin-20
zzzi-20
"""""

# Fourth, one of the three characteristics: Inheritance

class Grandfather:
    def piaochang(self):
        pass
class Father(Grandfather):#父类、基类
    def chifan(self):
        pass
    def hejiu(self):
        pass
    def shuijiao(self):
        pass

class Son (Father): # subclass, derived class 
    DEF WC (Self):
         Pass 
S = Son ()
s.chifan () # father in all functions can inherit 
s.piaochang () # father inherited the grandfather 
"" " "
If a function at the same time do not want to inherit the parent class is x can be rewritten in a subclass
Such default execution subclass
If the parent class subclasses may want to execute a method:
1.super (current class name, self) .x ()   
It is recommended to use super fixed wording
2 can also be used Father.x (self)
"" " 
# 2 multiple inheritance problem 
class F0:
     DEF A (Self):
         Print ( ' F0 ' )

class F1(F0):
    def a(self):
        print('F1')

class F2:
    def a(self):
        print('F2')

class S(F1,F2):
    pass
obj=S()
obj.a()
"""""
As a result F1
When multiple inheritance have the same function, select the first priority inheritance
Multiple inheritance can only be used in python and C ++
If F1 is not the same priority when looking in the parent class F1 in 
Priority is always the leftmost similar binary tree
Meanwhile priority subclass if both sides have a common parent, then finally found a common parent (root) in
"""

 

Guess you like

Origin www.cnblogs.com/zzzi/p/11478435.html