1 '''
2 self:
3 当前类的实例对象,但是在类中用self表示。
4 那个对象调用类中的函数时,该self就指向哪个对象
5
6 在本类中调用属性及方法,使用self.属性/方法的方式。
7
8 注:self不是关键字,换成其他的词语也可以,只要类中的成员方
9 法至少有一个形参即可,第一个形参就代表当前类的实例对象。但是一般
10 建议使用self。
11
12
13 __class__ :属性,代表当前类的类名。
14 '''
15 class Person():
16 name = "baby"
17 age = 0
18 def eatFood(self):
19 # 在本类中调用属性,使用self.属性。
20 print(self.name + "eat---food")
21
22 def sleep(self):
23 print("下雨天,睡觉天")
24 # 在本类中调用方法,使用self.方法 的方式。
25 self.shopping(100)
26
27 def shopping(self, money):
28 print("购物花了 %s 元" % money)
29
30 def printSelf(self):
31 print(self)
32 print(self.__class__)
33
34 # self:不是关键字,但帅/漂亮的人都用self。
35 # def run(abcdefg, num):
36 # print(num, abcdefg.name)
37
38 per1 = Person()
39 per1.name = "lucy"
40 per2 = Person()
41 per2.name = "PYC"
42 per1.eatFood()
43 per2.eatFood()
44 per2.sleep()
45 # per2.run(300)
46 per2.printSelf()
47 per1.printSelf()
48 print(per2)
49 print(per1)
50
51 per3 = Person()
52 per3.printSelf()
53
54 print('----------------------------------------')
55 print(per1.__class__)
56 print(per2.__class__)
57
58 list2 = list()
59 print(list2.__class__)
60
61 per2.printSelf()