1 class A:
2 __x=1
3 def __init__(self,name):
4 self.name=name
5
6 def __foo(self):
7 print('run foo')
8
9 def bar(self):
10 self.__foo()
11 print('run bar')
12
13 a=A('alice')
14 # print(a.__dict__)
15 # print(a.name)
16 # a.bar()
17 # # a.foo()
18 # a._A__foo()
19 # # print(a.__x)
20 # print(a._A__x)
21 # print(A.__dict__)
22
23 # a.bar()
24 # a.__foo()
25
26 # a.__x=1
27 # print(a.__dict__)
28 # print(a.__x)
29 # print(A.__dict__)
30
31 # A.__x=2
32 # print(A.__x)
33 # print(A.__dict__)
34
35 class Foo:
36 def __func(self):
37 print('from foo')
38
39 class Bar(Foo):
40 def __init__(self,name):
41 self.__name=name
42
43 def __func(self):
44 print('from bar')
45
46 # b=Bar()
47 # # b.func()
48 # print(Foo.__dict__)
49 # print(Bar.__dict__)
50
51 b=Bar('alice')
52 # print(b.__dict__)
53 # print(b.name)
54 # print(b._Bar__name)
55
56 class A:
57 def __foo(self): #_A__foo
58 print('A.foo')
59
60 def bar(self):
61 print('A.bar')
62 self.__foo() #self._A__foo() # 只调自己类的方法 定义时就已经确定好的!
63
64 class B(A):
65 def __foo(self): # _B_fooo
66 print('B.foo')
67
68 # b=B()
69 # b.bar()
70
71 # print(A.__dict__)
72 # print(B.__dict__)
73
74 class People:
75 def __init__(self,name,age):
76 self.__name=name
77 self.__age=age
78
79 def tell_info(self): #对接口 设定规则
80 print('name:<%s> age:<%s>'%(self.__name,self.__age))
81
82 def set_info(self,name,age):
83 if not isinstance(name,str):
84 print('名字必须是字符串类型')
85 return
86 if not isinstance(age,int):
87 print('年龄必须是数字类型')
88 return
89 self.__name=name
90 self.__age=age
91
92 p1=People('alice',12)
93 # print(p1.name,p1.age)
94 # print(p1.__dict__)
95 # p1.tell_info()
96 # p1.set_info('alex',18)
97 # p1.tell_info()
98
99 class ATM:
100 def __card(self):
101 print('插卡')
102
103 def __auth(self):
104 print('用户认证')
105
106 def __input(self):
107 print('输入取款金额')
108
109 def __print_bill(self):
110 print('打印账单')
111
112 def __take_money(self):
113 print('取款')
114
115 def withdraw(self):
116 self.__card()
117 self.__auth()
118 self.__input()
119 self.__print_bill()
120 self.__take_money()
121
122 # a=ATM()
123 # a.withdraw()
124
125 class Room:
126 def __init__(self,name,owner,weight,length,height):
127 self.name=name
128 self.owner=owner
129 self.__weight=weight
130 self.__length=length
131 self.__height=height
132
133 def tell_area(self):
134 return self.__weight * self.__length * self.__height
135
136 r=Room('客厅','alice',100,100,100)
137 # print(r.tell_area())