面向对象编程

复制代码
复制代码
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#类
class 类名:
    def func(self):
        print('类中的实例方法')
实例=类名()
实例.func()
  #封装.
# #把几个属性封装到对象中
class ren:
    def __init__(self,name,age,gender,hobby):
        self.name=name
        self.age=age
        self.gender=gender
        self.hobby=hobby
ren1=ren('小明',18,'男','玩耍')
print(ren1.name)
#继承
#派生类继承基类的属性和动作
class animal:
    huxi=True
    def chi(self):
        print('吃')
    def shui(self):
        print('睡')
    def he(self):
        print('喝水')
class cat(animal):
    def zhualaoshu(self):
        print('抓老鼠')
class dog(animal):
    def chaijia(self):
        print('拆家')
cat1=cat()
cat1.chi()
cat1.shui()
cat1.he()
cat1.zhualaoshu()
print(cat1.huxi)
#多态
#类中的实例方法a可以是任何类型的数据,
# 可以是官方给定的数据类型.可以是自己定义的类.
class foo:
    def func1(self,a):
        print(type(a))
        return type(a)
shili=foo()
shili.func1(True)
shili.func1(1)
shili.func1(1.2)
shili.func1("hahah")
shili.func1({12,123})
shili.func1([1,1,23,34])
shili.func1((1,2,3,4))
shili.func1({'k1':12,'k2':15})
shili.func1(cat1)
shili.func1(ren1)
 
#类方法
class Foo1:
    @classmethod
    def func(cls,a1):
        print(a1)
shili1=Foo1()
shili1.func('你好1')
Foo1.func('你好2')
 
#实例方法
class Foo2:
    def func(self,a1):
        print(a1)
shili2=Foo2()
shili2.func('你好3')
Foo2.func(shili2,'你好4')#用类强行执行实例方法,
 
#类和函数的区别:
#在面向对象编程中方法是类里面编写的函数
#1 定义时,实例方法至少需要一个self形参
#2 调用时,实例方法需要通过实例对象来调用
#在面向过程编程中,函数就是对某些动作和功能的封装
#1 定义时,可以不传参数
#2 调用时,只需要函数名()就可以
 
#__new__与__init__
#构造方法是__new__,会为对象创建一个内存空间,并把这个内存空间的地址返回给
#初始化方法__init__.
  
 
1. 面向对象帮你解决什么问题? 封装: 归类,将函数放置到一个类中. 打包,将数据打包放到一个对象.
2. 三大特性 封装 继承 多态,Python原生支持多态,崇尚鸭子模型.由于Python函数传参时,无序指定类型: def func(arg): # arg可以是多种类型,只要其中有send方法即可.
                    arg.send()
3. 编写面向对象程序
        归类+提取公共值
         
4. self到底是谁?
        self参数是Python帮助我们自动传递.
        如果执行面向对象中的方法时,前面必须有一个对象: xxx.func()
         
        class Foo:
            def f1(self):
                pass
                 
            def f2(self):
                self.f1()
                 
        obj = Foo()
        obj.f2()
     
5. Python支持多继承
6. 类的成员
         
        class Foo:
             
            # 方法
            def __init__(self,name):
                # 实例变量/字段
                self.name = name    
                 
            # 方法
            def func(self):
                pass
     
        # obj,Foo类的对象
        # obj,Foo类的实例
        obj = Foo('朱奎峰')
     
     
        成员共分为三类:
         
            变量:
                - 实例变量(字段)
                    - 公有实例变量(字段)
                    - 私有实例变量(字段)
                - 类变量(静态字段)
                    - 公有类变量(静态字段)
                    - 私有类变量(静态字段)
                     
                    实例一:
                        class Foo:
                            # 类变量(静态字段)
                            country = "中国"
                             
                            def __init__(self,name):
                                # 实例变量(字段)
                                self.name = name 
                             
                             
                            def func(self):
                                pass
                 
                 
                        obj1 = Foo('季红')
                        obj2 = Foo('王晓东')
                         
                        Foo.country
                         
                 
                思考题: 如何验证儿子都不知道私有字段的存在.
                 
                    无法访问:
                        class Base(object):
                            __secret = "受贿"
 
                        class Foo(Base):
 
                            def func(self):
                                print(self.__secret)
                                print(Foo.__secret)
 
 
                        obj = Foo()
                        obj.func()
                                         
                    可以访问:
                        class Base(object):
                            __secret = "受贿"
 
                            def zt(self):
                                print(Base.__secret)
 
 
                        class Foo(Base):
 
                            def func(self):
                                print(self.__secret)
                                print(Foo.__secret)
 
 
                        obj = Foo()
                        obj.zt()
                 
                     
            方法:
                - 实例方法
                        class Foo(object):
                            def __init__(self, name):
                                self.name = name
 
                            # 实例方法
                            def func(self):
                                print(self.name)
                                 
                        obj = Foo('..')
                        obj.func()
                         
                - 静态方法
                        class Foo(object):
                            def __init__(self, name):
                                self.name = name
 
                            # 静态方法,如果方法无需使用对象中封装的值,那么就可以使用静态方法
                            @staticmethod
                            def display(a1,a2):
                                return a1 + a2
                        Foo.display(1,3)
                         
                - 类方法
                        class Foo(object):
                         
                            # 类方法,cls是类
                            @classmethod
                            def show(cls,x1,x2):
                                print(cls,x1,x2)
 
                        # 执行类方法
                        Foo.show(1,8)
 
                面试题: 静态方法/类方法和实例方法的区别?
                 
                     
            属性(通过方法改造出来):
                示例:
                    class Foo(object):
                        def __init__(self):
                            pass
 
                        @property
                        def start(self):
                            return 1
 
                        @property
                        def end(self):
                            return 10
 
                    obj = Foo()
                    print(obj.start)
                    print(obj.end)
                    """
                        # 总结:
                        #     1. 编写时
                        #            - 方法上方写 @property
                        #            - 方法参数:只有一个self
                        #     2. 调用时:无需加括号  对象.方法
                        #     3. 应用场景: 对于简单的方法,当无需传参且有返回值时,可以使用 @property
  """7. 嵌套
             
            面向对象:
                """
                创建三个学校且三个学校的设施内容等都是一致.
                """
 
                class School(object):
                    def __init__(self, name, address):
                        self.name = name
                        self.address = address
 
                    def speech(self):
                        print('讲课')
 
                obj1 = School('北京校区', '昌平')
                obj2 = School('上海校区', '浦东')
                obj3 = School('深圳校区', '南山')
                class Teacher(object):
                    def __init__(self, name, age, salary):
                        self.name = name
                        self.age = age
                        self.__salary = salary
                        self.school = None
 
                t1 = Teacher('李', 19, 188888)
                t2 = Teacher('赵', 18, 60)
                t3 = Teacher('张',16, 900000)
                # ############## 老师分配校区
                t1.school = obj1
                t2.school = obj1
                t3.school = obj2
                # ####################################
                # 查看t1老师,所在的校区名称/地址
                print(t1.school.name)
                print(t1.school.address)
                print(t1.name)
                print(t1.age)
                t1.school.speech()
             
            准则: 字段和方法的归类.
         
            以前:
                [
                    {"name":'alex1','age':18,xxx:999},
                    {"name":'alex2',age':18},
                ]

  

posted @   冒蓝火的加特林哒哒哒  阅读(202)  评论(0编辑  收藏  举报
编辑推荐:
· 大模型 Token 究竟是啥:图解大模型Token
· 35岁程序员的中年求职记:四次碰壁后的深度反思
· 继承的思维:从思维模式到架构设计的深度解析
· 如何在 .NET 中 使用 ANTLR4
· 后端思维之高并发处理方案
阅读排行:
· BotSharp + MCP 三步实现智能体开发
· BotSharp 5.0 MCP:迈向更开放的AI Agent框架
· 分享 3 款基于 .NET 开源且免费的远程桌面工具
· 在线聊天系统中的多窗口数据同步技术解密
· 5. RabbitMQ 消息队列中 Exchanges(交换机) 的详细说明
点击右上角即可分享
微信分享提示