day31

一、面向过程

面向过程:不是一门技术,是一种编程思想,其核心是过程两个字。过程就是先干什么,再干什么,最后干什么,类似于机器师思维

案例:

    把大象关进冰箱

    1、打开冰箱

    2、把大象放进去

    3、关闭冰箱

优点:复杂的问题流程化,简单化

缺点:扩展性差,可维护性差

 1 """  
 2  问题:
 3         实现用户注册:
 4             1.输入用户名密码
 5             2.验证参数
 6             3.注册
 7 """
 8 # 1 与用户交互
 9 def interactive():
10     username = input("请输入用户名:")
11     password = input("请输入密码:")
12     email = input("请输入email:")
13 
14     return {
15         'username': username,
16         'password': password,
17         'email': email,
18     }
19 
20 
21 # 2. 验证参数
22 def check_info(userinfo):
23     is_valid = True
24 
25     if len(userinfo['username']) == 0:
26         print("用户名不能为空")
27         is_valid = False
28 
29     if len(userinfo['password']) == 0:
30         print("密码不能为空")
31         is_valid = False
32     if len(userinfo['email']) == 0:
33         print("email不能为空")
34         is_valid = False
35 
36     return {
37         'is_valid': is_valid,
38         'userinfo': userinfo
39     }
40 
41 
42 # 3. 注册
43 import json
44 
45 
46 def register(param):
47     if param['is_valid']:
48         with open('userinfo', 'w', encoding='utf-8') as f:
49             json.dump(param['userinfo'], f)

二、面向对象

面向对象:核心是对象两字

1、程序里面:

  对象就是盛放数据属性和功能的容器

2、现实中:

  对象就是特征与技能的结合体

优点:扩展性强

缺点:编程复杂程度高

应用场景:对扩展性要求较高的场景,比如:QQ,微信

 1 #版本1:
 2 stu1_name = 'egon'
 3 stu2_name = 'tom'
 4 stu1_age = 18
 5 stu2_age = 20
 6 stu1_gender = 'male'
 7 stu2_gender = 'male'
 8 stu1_courses = []
 9 stu2_courses = []
10 
11 def choose_course(stu_name, stu_courses,course):
12     stu_courses.append(course)
13     print("%s 选课成功 %s" % (stu_name, stu_courses))
14 
15 choose_course(stu1_name ,stu1_courses, 'python全站开发')
16 choose_course(stu2_name ,stu2_courses, 'linux全站开发')
17 
18 
19 # 版本2
20 
21 stu1 = {
22     'name':'egon',
23     'age' : 18,
24     'gender' : 'male',
25     'courses': []
26 }
27 
28 stu2 = {
29     'name':'tom',
30     'age' : 18,
31     'gender' : 'male',
32     'courses': []
33 }
34 
35 
36 def choose_course(stu_dic,course):
37     stu_dic['courses'].append(course)
38     print("%s 选课成功 %s" % (stu_dic['name'], stu_dic['courses']))
39 
40 choose_course(stu1, 'python全站开发')
41 choose_course(stu2, 'linux全站开发')
42 
43 # 版本3
44 
45 def choose_course(stu_dic,course):
46     stu_dic['courses'].append(course)
47     print("%s 选课成功 %s" % (stu_dic['name'], stu_dic['courses']))
48 
49 stu1 = {
50     'name':'egon',
51     'age' : 18,
52     'gender' : 'male',
53     'courses': [],
54     'choose_course':choose_course
55 }
56 
57 stu2 = {
58     'name':'tom',
59     'age' : 18,
60     'gender' : 'male',
61     'courses': [],
62     'choose_course':choose_course
63 }
64 
65 
66 stu1['choose_course'](stu2, 'python开发')
67 stu2['choose_course'](stu2, 'linux开发')

 三、类的概念

对象:特征和技能的结合体

类:一系列对象相似的特征和相似的技能结合体

强调:站在不同的角度,划分的分类是不一样的

问题:现有类还是对象?

1、现实中:

  必须先有对象再有类

2、程序中:

  必须先定义类,再调用类产生对象

 

定义类:

类定义阶段发生了什么事?

   1.立即执行类体代码
2.产生了类的名称空间,把类里面定义的名字都扔到字典里
3.把类的名称空间绑定给类名 类名.__dict__
 1 # 定义类
 2 """
 3 class 类名():
 4     pass
 5     
 6     
 7 def 函数名():
 8     pass
 9     
10 类名: 一般是首字母大写
11 """
12 
13 class Student():
14     school = 'SH'
15 
16     def choose_course(stu_dic, course):
17         stu_dic['courses'].append(course)
18         print("%s 选课成功 %s" % (stu_dic['name'], stu_dic['courses']))
19 
20 # 类的名称空间
21 print(Student.__dict__)
22 
23 print(Student.__dict__)
24 #
25 #
26 # # 造对象,调用类,产生对象
27 stu1 = Student()  # 调用类 产生空对象
28 # stu2 = Student()
29 #
30 print(stu1.__dict__)

了解知识:调用类的过程就是实例化,得到的对象就是一个实例。

四、定制对象自己独有的属性

 1 # 版本3
 2 
 3 
 4 """
 5     产生对象发生了几件事?
 6         1. 产生一个空对象,并且把该空对象当成第一个参数传递
 7 """
 8 
 9 class Student():
10     school = 'SH'
11 
12     # 初始化方法
13     def __init__(stu_obj, name, age, gender, courses=[]):
14         stu_obj.name = name  # stu1.__dict__['name'] = 'egon'
15         stu_obj.age = age  # stu1.__dict__['age'] = 20
16         stu_obj.gender = gender  # stu1.__dict__['gender'] = 'male'
17         stu_obj.courses = courses  # stu1.__dict__['courses'] = []
18 
19     def choose_course(stu_dic, course):
20         stu_dic['courses'].append(course)
21         print("%s 选课成功 %s" % (stu_dic['name'], stu_dic['courses']))
22 
23 
24     # print(123)
25 
26 
27 # 类的名称空间
28 # print(Student.__dict__)
29 stu1 = Student('egon', 18 ,'male')
30 
31 print(stu1.__dict__)
32 # stu2 = Student()
33 
34 # stu1.name = 'egon'      # stu1.__dict__['name'] = 'egon'
35 # stu1.age = 18           # stu1.__dict__['age'] = 20
36 # stu1.gender = 'male'    # stu1.__dict__['gender'] = 'male'
37 # stu1.courses = []       # stu1.__dict__['courses'] = []
38 #
39 #
40 # stu2.name = 'tom'      # stu2.__dict__['name'] = 'egon'
41 # stu2.age = 18           # stu2.__dict__['age'] = 20
42 # stu2.gender = 'male'    # stu2.__dict__['gender'] = 'male'
43 # stu2.courses = []       # stu2.__dict__['courses'] = []
44 
45 
46 #
47 # init(stu1, 'egon', 18, 'male')
48 # init(stu2, 'tom', 18, 'male')
49 #
50 #
51 # print(stu1.__dict__)
52 # print(stu2.__dict__)

五、属性查找

 1 class Student():
 2     school = 'SH'
 3     # 初始化方法
 4     def __init__(stu_obj, name, age, gender, courses=[]):
 5         stu_obj.name = name  # stu1.__dict__['name'] = 'egon'
 6         stu_obj.age = age  # stu1.__dict__['age'] = 20
 7         stu_obj.gender = gender  # stu1.__dict__['gender'] = 'male'
 8         stu_obj.courses = courses  # stu1.__dict__['courses'] = []
 9 
10     def choose_course(self, course):
11         self.courses.append(course)
12         print("%s 选课成功 %s" % (self.name, self.courses))
13 
14 
15     # print(123)
16 
17 
18 # 类的名称空间
19 # print(Student.__dict__)
20 stu1 = Student('egon', 18 ,'male')
21 stu2 = Student('tom', 18 ,'male')
22 
23 # print(stu1.__dict__)
24 
25 # 1 类的属性查找
26 # print(Student.__dict__['school'])
27 
28 # # Student.__dict__['xxx'] = 'xxx'
29 # del Student.__dict__['xxx']
30 
31 # Student.xxx = 'xxx'
32 #
33 # del  Student.xxx
34 
35 # Student.school = 'MMM'
36 # print(Student.school)
37 
38 # print(Student.choose_course)
39 # Student.choose_course(stu1, 'python')
40 
41 
42 # 对象操作属性  点语法取值,先从自己对象中取,如果没有,在取类中取值
43 # print(stu1.school)
44 #
45 # stu1.xxx = 'x'
46 # del  stu1.xxx
47 # stu1.school = 'y'
48 
49 # Student.school = 'x'
50 # print(stu1.school)
51 
52 
53 # 类中的属性是共享给所有对象的,但是,类中的方法是给对象用的, 并且,把对象自己当成第一个参数传递
54 # stu1.school = 'y'
55 # print(stu1.school)
56 #
57 # print(stu2.school)
58 
59 stu1.choose_course('python')

小练习

要求:

1、定义一个学生类,产生一堆对象

2、增加一个计数器,记录产生了多少个对象

 1 class Student():
 2     school = 'school'
 3     count = 0
 4 
 5 
 6     def __init__(self, name, age, gender, courses=[]):
 7         self.name = name
 8         self.age = age
 9         self.gender = gender
10         self.courses = courses
11         Student.count+=1
12 
13     def choice_course(self, course):
14         self.courses.append(course)
15         print('%s 选修 %s' % (self.name, course))
16 
17 stu1 = Student('jack', 23, 'male')
18 stu2 = Student('tom', 24, 'male')
19 stu3 = Student('bob', 24, 'male')
20 stu1.choice_course('python')
21 print(Student.count)

作业:

要求:

1、英雄要有昵称,生命值,攻击力

2、产生两个对象

3、互相攻击,生命值小于0,判定为死亡

 1 class Hero():
 2     def __init__(self, name, life_value, damage):
 3         self.name = name
 4         self.life_value = life_value
 5         self.damage = damage
 6 
 7     def fight(self, h1, h2):
 8         while True:
 9             h1.life_value -= h2.damage
10             h2.life_value -= h1.damage
11             if h1.life_value <= 0 and h2.life_value >= 0:
12                 print('托儿索死亡')
13                 break
14             elif h2.life_value <= 0 and h1.life_value >= 0:
15                 print('儿童劫死亡')
16                 break
17             elif h1.life_value <= 0 and h2.life_value <=0:
18                 print('同归于尽')
19                 break
20             else:
21                 print('正在战斗中。。。')
22 
23 
24 hero1 = Hero('托儿索', 100, 91)
25 hero2 = Hero('儿童劫', 100, 81)
26 Hero.fight(Hero, hero1, hero2)

 

posted @ 2021-07-12 20:38  Gnomeshghy  阅读(49)  评论(0)    收藏  举报