程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Definition and use of Python classes

編輯:Python

Class definition

# 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>

Class method definition

# 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

Class inheritance

# 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'>)

Class , private

# 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

The subclass calls the method of the parent class

# 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 

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved