# coding:utf-8
if __name__ == '__main__':
''' Definition :class Class name (object): Class properties , Such method ...... Instantiation : Class name ( No arguments | Parameters ......) '''
class A(object):
pass
a = A()
print(a) # <__main__.A object at 0x00D9DD50>
# coding:utf-8
if __name__ == '__main__':
''' Definition : adopt def + Method name (self, Parameters 1, Parameters 2......) self Is a must call :object. Method name ( Parameters 1, Parameters 2......) '''
class B(object):
sex = 'man'
def talk(self, name):
print(name)
b = B()
b.talk('ok') # ok
# coding:utf-8
if __name__ == '__main__':
''' When declaring a class, specify which classes this class inherits class Class name (extend1,extend2.......): attribute , Method ...... extend1,extend2 Represents the class to inherit . You can inherit multiple classes at once The order of inheritance is from left to right , If the inherited method or property is repeated , To inherit first class.__mro__ View the inheritance chain of a class '''
class C(object):
sex = 'woman'
def see(self, name):
print(f' see {
name}')
def talk(self, name):
print(f' say {
name}')
class D(B, C):
pass
d = D()
d.talk('yes') # yes No say yes To inherit first
d.see(' book ') # Reading a book
print(d.sex) # man No woman To inherit first
print(D.__mro__) # (<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class 'object'>)
# coding:utf-8
if __name__ == '__main__':
''' Private property definition : __+ Variable name Private method definition : __+ Function name (self, Parameters 1, Parameters 2......) Private external cannot access , Only internal access . But private attributes can be passed externally object._+ Class name __+ Variable name Forced access '''
class E(object):
__name = 'xie'
sex = 'man'
def __talk(self, name):
print(name)
def run(self):
self.__talk('ok')
def dp(self):
print(self.__name)
e = E()
# print(e.__name) Error External cannot access private properties D
print(e.sex) # man
# e.__talk('yes') Error Private methods cannot be accessed externally
# object._+ Class name __+ Variable name Forced access
print(e._E__name) # xie
e.run() # ok
e.dp() # xie
# coding:utf-8
if __name__ == '__main__':
''' adopt super(). Method ( Parameters 1, Parameters 2......) call , This usage requires python edition 3 above or supper( Subclass name ,self). Method ( Parameters 1, Parameters 2......) '''
class F(object):
def talk(self, name):
print(f'super name is {
name}')
class G(F):
def talk(self, children_name, super_name):
self.print_children_name(children_name)
super().talk(super_name)
def talk2(self, children_name, super_name):
self.print_children_name(children_name)
super(G, self).talk(super_name)
def print_children_name(self, name):
print(f'children name is {
name}')
g = G()
g.talk(' Xiao Ming ', ' Daming ') # children name is Xiao Ming super name is Daming
g.talk2(' Xiao Ming ', ' Daming ') # children name is Xiao Ming super name is Daming
problem : In image processing
Its tumbling today django When