面向对象

编程范式(流派):
面向对象编程,面向过程编程 各有用处!!
编程:语法+数据结构(list dict)+算法(逻辑)
-----------------------------------
1.面向过程编程:核心是过程二字,过程指的是解决问题的步骤,设计一条流水线,机械式的思维方式

优点:复杂的问题流程化,进而简单化---系统监控脚本,自动部署脚本之类的,eg:软件包解压安装(不再需要扩展了)就可以使用面向过程的思维编写代码
缺点:可扩展性差,牵一发而动全身
 1 # 注册
 2 import json
 3 import re
 4 
 5 def interative():
 6     name = input('>>:').strip()
 7     password = input(">>").strip()
 8     email = input('>>').strip()
 9     return {
10         'name': name,
11         'password': password,
12         'email': email
13     }
14 
15 
16 def check(user_info):
17     is_valid = True
18 
19     if len(user_info['name']) == 0:
20         print('用户名不能为空')
21         is_valid = False
22 
23     if len(user_info['password']) < 6:
24         print('密码不能少于6位')
25         is_valid = False
26 
27     if not re.search(r'@.*.com$', user_info['email']):
28         print('邮箱格式不合法')
29         is_valid = False
30 
31     return {
32         'is_valid': is_valid,
33         'user_info': user_info
34     }
35 
36 
37 def register(check_info):
38     if check_info['is_valid']:
39         with open('db.json', 'w', encoding='utf-8') as f:
40             json.dump(check_info['user_info'], f)
41 
42 
43 def main():
44     user_info = interative()
45 
46     check_info = check(user_info)
47 
48     register(check_info)
49 
50 
51 if __name__ == '__main__':
52     main()

2.面向对象编程:核心就是对象二字,对象就是特征与技能的结合体 --站在上帝的角度想问题,一切事物都是对象!
优点:可扩展性强
缺点:编程复杂度高
应用场景:用户需求经常变化,互联网应用,游戏,企业内部应用


类就是一系列对象相似的特征与技能的结合体
强调:站在不同的角度,得到的分类是不一样的

在现实世界中:一定先有对象,后有类
在程序中:一定得先定义类,后调用类来产生对象

站在路飞学院的角度,大家都是学生

在现实世界中:
对象1:王二丫
特征:
学校='luffycity'
名字='王二丫'
性别='女'
年龄=18
技能:
学习
吃饭
睡觉

对象2:李三炮
特征:
学校='luffycity'
名字='李三炮'
性别='男'
年龄=38
技能:
学习
吃饭
睡觉

对象3:张铁蛋
特征:
学校='luffycity'
名字='张铁蛋'
性别='男'
年龄=48
技能:
学习
吃饭
睡觉

总结现实中路飞学院的学生类:
相似的特征
学校='luffycity'

相似的技能
学习
吃饭
睡觉
 1 # 先定义类:
 2 class LuffyStudent:
 3     school = 'luffycity'
 4 
 5     def learn(self):
 6         print('is learning')
 7 
 8     def eat(self):
 9         print('is eatting')
10 
11 # 后产生对象
12 stu1 = LuffyStudent
13 stu2 = LuffyStudent()
14 stu3 = LuffyStudent()
15 
16 print(stu1)
17 print(stu2)
18 print(stu3)
3.如何使用类
类是定义就运行
函数是调用才运行

类的用途:
1.操作它的属性 增删改查
2.实例化 产生对象
 1 class LuffyStudent:
 2     school='luffycity' # 数据属性
 3 
 4     def learn(self):  # 函数属性
 5         print('is learning')
 6 
 7     def eat(self):  # 函数属性
 8         print('is sleeping')
 9     # print('---')  # 类是定义,就运行
10 
11 #查看类的名称空间
12 # print(LuffyStudent.__dict__)
13 # print(LuffyStudent.__dict__['school'])
14 # print(LuffyStudent.__dict__['learn'])
15 
16 #
17 # print(LuffyStudent.school)
18 # print(LuffyStudent.learn)
19 
20 #
21 LuffyStudent.country = 'china'
22 print(LuffyStudent.__dict__)
23 # print(LuffyStudent.country)
24 
25 #
26 del LuffyStudent.country
27 print(LuffyStudent.__dict__)
28 
29 #
30 LuffyStudent.school = 'LuffyCity'
31 print(LuffyStudent.__dict__)

4.__init__方法
__init__方法用来为对象定制对象自己独有的特征
__init__实例化对象时会自动调
 1 class LuffyStudent:
 2     school = 'luffycity'
 3 
 4     def __init__(self,name,sex,age):
 5         self.Name=name
 6         self.Sex=sex
 7         self.Age=age
 8 
 9     def learn(self):
10         print('is learning')
11 
12     def eat(self):
13         print('is eatting')
14 
15 # 后产生对象
16 stu1 = LuffyStudent('alice','',18)
17 
18 #加上__init__方法后,实例化的步骤
19 #1.先产生一个空对象stu1
20 #2.触发函数 LuffyStudent.__init__(stu1,name,sex,age)
21 
22 #
23 # print(stu1.__dict__)
24 # print(stu1.Name)
25 # print(stu1.Age)
26 
27 #
28 stu1.Name = 'alex'
29 print(stu1.__dict__)
30 print(stu1.Name)
31 
32 #
33 del stu1.Name
34 print(stu1.__dict__)
35 
36 #
37 stu1.class_name = 'python开发'
38 print(stu1.__dict__)
39 
40 stu2 = LuffyStudent('lily','',30) # Luffycity.__init__(stu2,'lily','男',30)
41 print(stu2.__dict__)
42 print(stu2.Name)
5.属性查找
 1 # x = 'global'
 2 class LuffyStudent:
 3     school = 'luffycity'
 4 
 5     def __init__(self,name,sex,age):
 6         self.Name=name
 7         self.Sex=sex
 8         self.Age=age
 9 
10     def learn(self,x):
11         print('%s,%s is learning' % (self.Name,x))
12 
13     def eat(self):
14         print('%s is eatting' % self.Name)
15 
16 # 后产生对象
17 stu1 = LuffyStudent('alice','',18)
18 stu2 = LuffyStudent('alex','',38)
19 stu3 = LuffyStudent('lily','',40)
20 # print(stu1.__dict__)
21 # print(stu2.__dict__)
22 # print(stu3.__dict__)
23 
24 # 对象:特征与技能的结合体
25 # 类:类是一系列对象相似的特征与相似的技能的结合体
26 
27 # 类中的数据属性:是所有对象共有的
28 # print(LuffyStudent.school)
29 #
30 # print(stu1.school,id(stu1.school))
31 # print(stu2.school,id(stu2.school))
32 # print(stu3.school,id(stu3.school))
33 
34 # 类中的函数属性:是绑定给对象使用的,绑定到不同的对象是不同的绑定方法,对象调用绑定方法时,会把对象本身当作第一个传入,传给self
35 # print(LuffyStudent.learn)
36 # LuffyStudent.learn(stu1)
37 # LuffyStudent.learn(stu2)
38 # LuffyStudent.learn(stu3)
39 
40 # print(stu1.learn)
41 # stu1.learn(1) #learn(stu1,4)
42 # print(stu2.learn)
43 # print(stu3.learn)
44 
45 # stu2.learn(2)
46 # stu3.learn(3)
47 
48 # 属性查找,对象会在自己内部找 --》类中找--》父类找--》不会去全局找
49 # stu1.x = 'form stu1'
50 # LuffyStudent.x = 'from LuffyCity class '
51 # print(LuffyStudent.__dict__)
52 # print(stu1.__dict__)
53 # print(stu1.x)
6.补充知识
1.站的角度不同,定义出的类是截然不同的;
2.现实中的类并不完全等于程序中的类,比如现实中的公司类,在程序中有时需要拆分成部门类,业务类等;
3.有时为了编程需求,程序中也可能会定义现实中不存在的类,比如策略类,现实中并不存在,但是在程序中却是一个很常见的类。

4.python 一切皆对象,在python3里统一了类与类型(list dict)的概念
 1 # print(type([1,2]))
 2 # print(list)
 3 class LuffyStudent:
 4     school = 'luffycity'
 5 
 6     def __init__(self,name,sex,age):
 7         self.Name=name
 8         self.Sex=sex
 9         self.Age=age
10 
11     def learn(self,x):
12         print('%s,%s is learning' % (self.Name,x))
13 
14     def eat(self):
15         print('%s is eatting' % self.Name)
16 # print(LuffyStudent)
17 
18 li1 = [1,2,3] # li = list(1,2,3) # list 对象
19 li1.append(4) # 对象在调自己的绑定方法 # list.append(li1,4)
20 # list.append(li1,4) # 类中的方法 是给 对象用的
21 print(li1)
7.可扩展性高
 1 class Chinese:
 2     country = 'China'
 3     def __init__(self,name,age,sex):
 4         self.name=name
 5         self.age=age
 6         self.sex=sex
 7     def eat(self):
 8         print('%s is eating' % self.name)
 9 
10 p1 = Chinese('alice',19,'')
11 p2 = Chinese('alex',22,'')
12 
13 print(p1.name,p1.country)
14 print(p2.name,p2.country)
15 p1.eat()
16 p2.eat()


原文地址:https://www.cnblogs.com/alice-bj/p/8530411.html