12-面向对象1
发布日期:2021-05-14 21:58:05 浏览次数:8 分类:博客文章

本文共 6609 字,大约阅读时间需要 22 分钟。

���������

���������������������������������

class ���������

������������������

 

demo���������������Car���

In [1]: #������������Car���In [2]: class Car():   ...:     #������   ...:     def getCarInfo(self):   ...:         print('���������������������%d���������������%s'%(self.wheelNum,self.color))   ...:     def move(self):   ...:         print('���������������...')

���������

������1.���������������2���������������������������������������Car���������������������Car(object)������������������

������2.���������������������������������������

������������

���������������������������������Car������������������������������������������������������������������������������������������������������

���python���������������������������������������������������������������������������������������������������

��������������� = ������()

������������demo���

In [1]: #������������Car���In [2]: class Car():   ...:     #������   ...:     def getCarInfo(self):   ...:         print('���������������������%d���������������%s'%(self.wheelNum,self.color))   ...:     def move(self):   ...:         print('���������������...')   ...:         In [3]: #���������������������������������BMW���������������������In [4]: BMW =  Car()In [5]: BMW.color = '������'In [6]: BMW.wheelNum = 4In [7]: BMW.move()���������������... In [8]: print(BMW.color) ������ In [9]: print(BMW.wheelNum) 4

���������

������1���BMW = Car()���������������������������Car���������������������������������������������������BMW���������������������������

������2������������������BMW.color = '������'���������BMW���������������������������������������������������BMW.color = xxx ���������������������������������

������3���BMW������������������������������������������������������������������

������4������������������������������������������������������������������������������

__init__������

���������������demo���������������������BMW������������������2������������wheelNum���color���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

__init__������

<1>������������

Class ������:     #���������������������������������������������������     def __init__(self):         pass

<2>__init__���������������

11 #���������������           12 class Car():          13                       14     def __init__(self):                                                                              15         self.wheelNum = 4 16         self.color = '������' 17     def move(self):   18         print('������������...') 19                       20 #������������             21 BMW = Car()           22 print('������������������%s'%BMW.color) 23 print('������������������%d'%BMW.wheelNum)

���������

������������������Car���������������������������__init__()���������������������BMW������������������2���������������������__init__()���������������������������������������������������

���������

���������������������������__init__()������������������������������������������������������������������������__init__()���������������������������������������������������������������������������������

11 #��������������� 12 class Car(): 13   14     def __init__(self,wheelNum,color): 15         self.wheelNum = wheelNum 16         self.color = color 17     def move(self): 18         print('������������...') 19   20 #������������ 21 BMW = Car(4,'������')                                                                                  22 print('������������������%s'%BMW.color) 23 print('������������������%d'%BMW.wheelNum) python3 test.py ������������������������ ������������������4

���������

������1���__init__()������������������������������������������������������������������������������

������2���__init__(self)������ ���������1������������������slef������������������������������������2������������������__init__(self)���������self���������������������������������2������������������__init__(self,x,y)

������3���__init__(self)������self������������������������������������python������������������������������������������������������

������������

������������������������������

11 #��������������� 12 class Car(): 13       14     def __init__(self,wheelNum,color):                                                               15         self.wheelNum = wheelNum 16         self.color = color 17     def move(self): 18         print('������������...') 19       20 #������������ 21 BMW = Car(4,'������')��� 22 print(BMW)������������#���������������������BMW������print������������ 23 print('������������������%s'%BMW.color) 24 print('������������������%d'%BMW.wheelNum)

������������������

python3 test.py<__main__.Car object at 0x7f2b87338940>���������#���������������������������������BMW���������������������������������������������������������������������4

__str__()������

11 #���������������          12 class Car():         13                      14     def __init__(self,wheelNum,color): 15         self.wheelNum = wheelNum 16         self.color = color 17     def move(self):  18         print('������������...') 19                      20     def __str__(self): 21         return "���������������������%s������������������%d"%(self.color,self.wheelNum)                           22                     23 #������������            24 BMW = Car(4,'������')  25 print(BMW)           26 print('������������������%s'%BMW.color)  27 print('������������������%d'%BMW.wheelNum)

���������

������1������python���������������������__xxxx__()������ ������������������������������������������������������������

������2������������print���������������������������������������������__str__(self)������������������������������������������������return���������

������self

������������������

11 #���������������  12 class Animal():       13              14     #������    15     def __init__(self,name):          16         self.name = name              17              18     def printName(self):              19         print('���������:%s'%(self.name)) 20              21 #������������������ 22 def myPrint(animal):  23     animal.printName() 24              25 dog1 = Animal('������') 26 myPrint(dog1) 27 dog2 = Animal('������') 28 myPrint(dog2)

���������������

���������:���������������:������

���������

������1������������self������������������������

������2������������self������C++���������������this������������������������������������������������

������3������������������������������������python���������������������������������������������������������self������������������������������������������������������

上一篇:投资之番外篇
下一篇:MySql之Explain

发表评论

最新留言

不错!
[***.144.177.141]2025年04月18日 04时57分56秒