面向对象编程
python面向对象编程
面向对象编程思想引入
猫狗大战
# 描述猫和狗
# 方式一:使用字典
cat_one = {
'nickname': 'cookie'
'type':'Rag_doll'
'attack_val':20
'life_val':400
}
cat_two = {
'nickname': 'cocoa'
'type':'Persian'
'attack_val':30
'life_val':500
}
cat_three = {
'nickname': 'Huahua'
'type':'dragon_Li'
'attack_val':180
'life_val':800
}
dog_one = {
'nickname': 'Xiaohei'
'type':'Chinese pastoral dog'
'attack_val':600
'life_val':1000
}
dog_two = {
'nickname': 'Tompson'
'type':'British rural dog'
'attack_val':150
'life_val':500
}
# 方式二:封装成函数
def get_cat(nickname,cat_type,attack_val,life_val):
data_list = {
nickname: name,
cat_type: cat_type,
attack_val: attack_val,
life_val: life_val
}
return data_list
def get_dog(nickname,dog_type,attack_val,life_val):
data_list = {
nickname: name,
dog_type: cat_type,
attack_val: attack_val,
life_val: life_val
}
return data_list
cat_one = get_cat('cookie', 'Rag_doll', 20, 400)
cat_two = get_cat('cocoa', 'Persian', 30, 500)
cat_three = get_cat('Huahua', 'dragon_Li', 180, 800)
dog_one = get_cat('xiaohei', 'Chinese pastoral dog', 600, 1000)
dog_two = get_cat('Tom', 'British rural dog', 150, 500)
def dog_attack(dog_obj,cat_obj):
print('当前猫的剩余生命值为:%s' % cat_obj.get('life_val'))
cat_obj.get['life_val'] = -= dog_obj.get('attack_val')
print("""狗:%s攻击了猫:%s一次,猫掉血:%s剩余生命值:%s""" % (dog_obj.get('nickname'), cat_obj.get('nickname'), dog_obj.get('attack_val'), cat_obj.get('life_val')))
def cat_attack(cat_obj,dog_obj):
print('当前狗的剩余生命值为:%s' % dog_obj.get('life_val'))
dog_obj.get['life_val'] = -= cat_obj.get('attack_val')
print("""猫:%s攻击了狗:%s一次,狗掉血:%s剩余生命值:%s""" % (cat_obj.get('nickname'), dog_obj.get('nickname'), cat_obj.get('attack_val'), dog_obj.get('life_val')))
# 狗攻击猫
dog_attack(dog_one, cat_one)
print(cat_one)
# 猫攻击狗
cat_attack(cat_three, dog_two)
print(dog_two)
"""如何做到只有猫可以调用猫的攻击动作,狗只能调用狗的攻击动作"""
# 让猫的数据跟猫的功能绑定,狗的数据跟狗的功能绑定
def get_cat(nickname, cat_type, attack_val, life_val):
# 将猫的攻击动作放在产生猫的函数内
def cat_attack(cat_obj, dog_obj):
"""
cat_obj: 接收一个猫
dog_obj: 接收一条狗
"""
print('当前狗的血量是:%s' % dog_obj.get('life_val'))
dog_obj['life_val'] -= cat_obj.get('attack_val')
print("""猫:%s 抓了狗:%s 一 爪,狗掉血:%s 剩余血量:%s""" % (
cat_obj.get('nickname'), dog_obj.get('nickname'), cat_obj.get('attack_val'), dog_obj['life_val']))
data_dict = {
'nickname': nickname,
'cat_type': cat_type,
'attack_val': attack_val,
'life_val': life_val,
'cat_attack': cat_attack
}
return data_dict
def get_dog(nickname, dog_type, attack_val, life_val):
def dog_attack(dog_obj, cat_obj):
"""
dog_obj: 接收一条狗
cat_obj: 接收一个猫
"""
print('当前猫的血量是:%s' % cat_obj.get('life_val'))
cat_obj['life_val'] -= dog_obj.get('attack_val')
print("""狗:%s 咬了猫:%s 一口 猫掉血:%s 剩余血量:%s""" % (
dog_obj.get('nickname'), cat_obj.get('nickname'), dog_obj.get('attack_val'), cat_obj['life_val']))
data_dict = {
'nickname': nickname,
'dog_type': dog_type,
'attack_val': attack_val,
'life_val': life_val,
'dog_attack': dog_attack
}
return data_dict
cat_one = get_cat('cookie', 'Rag_doll', 20, 400)
cat_two = get_cat('cocoa', 'Persian', 30, 500)
dog_one = get_dog('xiaohei', 'Chinese pastoral dog', 600, 1000)
dog_two = get_dog('Tom', 'British rural dog', 150, 500)
cat_one['cat_attack'](cat_one, dog_one)
dog_one['dog_attack'](dog_one, cat_two)
"""
上述操作将数据与功能进行绑定
不再是所有的数据都可以任意调用功能
"""
# 上述将数据与功能整合到一起的操作其实就是面向对象编程
编程思想的分类
-
面向过程编程
将程序的执行流程化,即从上到下分步操作,分步的过程中解决问题。 -
面向对象编程
核心就是'对象'二字;对象其实就是一个"容器"。容器将数据与功能整合到一起。
类与对象的概念
- 类
类即类别、种类 相当于诸多对象公有的特征、数据和功能。 - 对象
对象即数据与功能的结合体
类与对象的创建
"""
在代码编程中必须先有类才能有对象
"""
# 先定义类,后产生对象
# 学生类
class Student:
# 学生类公共的数据
school = '衡水中学'
# 学生类公共的功能
def test_registration_system(self):
print('学生考试报名系统')
"""
语法结构
class 类名:
类属代码
1.class是定义类的关键字
2.类名类似于函数名,首字母推荐大写,便于区分
3.类体代码就是存放对象公共数据和功能的地方
数据: 变量名 = 变量的值
功能: 函数
"""
# 查看名称空间的方法>>>:__dict__
print(Student.__dict__) # {'__module__': '__main__', 'school': '衡水中学', 'test_registration_system': <function Student.test_registration_system at 0x000000000261CBF8>, '__dict__': <attribute '__dict__' of 'Student' objects>, '__weakref__': <attribute '__weakref__' of 'Student' objects>, '__doc__': None}
print(Student.__dict__['school']) # 获取类中的属性:衡水中学
print(Student.__dict__['choose_course']) # 获取类中的方法:<function Student.test_registration_system at 0x000000000261CBF8>
"""类获取数据和功能有一个简便方式:句点符"""
print(Student.school)
print(Student.test_registration_system)
"""如何产生对象:类名加括号"""
obj1 = Student() # 类名加括号就是在产生一个对象
obj2 = Student() # 类名加括号就是在产生一个对象
# 类名加括号就是在产生一个对象
print(obj1.__dict__, obj2.__dict__) # {} {}
print(obj1.school) # 衡水中学
print(obj2.school) # 衡水中学
print(obj1.test_registration_system) # <bound method Student.test_registration_system of <__main__.Student object at 0x00000000021C8518>>
print(obj2.test_registration_system) # <bound method Student.test_registration_system of <__main__.Student object at 0x00000000021C83C8>>
print(obj1.school) # 衡水中学
print(obj2.school) # 衡水中学
Student.school = '毛坦厂中学'
print(obj1.school) # 毛坦厂中学
print(obj2.school) # 毛坦厂中学
对象的实例化
# 学生类
class Student:
def __init__(self, name, age, gender):
"""方法为功能给对象添加独有的数据"""
self.name = name # obj.__dict__['name'] = name
self.age = age # obj.__dict__['age'] = age
self.gender = gender # obj.__dict__['gender'] = gender
def set_info(self, name, age, gender):
"""方法为功能给对象添加独有的数据"""
self.name = name # obj.__dict__['name'] = name
self.age = age # obj.__dict__['age'] = age
self.gender = gender # obj.__dict__['gender'] = gender
# 学生类公共的数据
school = '衡水中学'
# 学生类公共的功能
def test_registration_system(self):
print('学生考试报名系统')
# obj1 = Student() # 目前对象没有自己独有的属性 __init__() missing 3 required positional arguments: 'name', 'age', and 'gender'
# obj2 = Student() # 目前对象没有自己独有的属性 __init__() missing 3 required positional arguments: 'name', 'age', and 'gender'
# print(obj1.__dict__) # 给字典添加键值对
# print(obj2.__dict__) # 给字典添加键值对
'''方案1:逐步给对象添加独有的数据'''
obj1 = Student(1, 2, 3)
obj2 = Student(4, 5, 6)
obj1.__dict__['name'] = 'tom' # obj1.name = 'tom'
obj1.__dict__['age'] = 18 # obj1.age = 18
obj1.__dict__['gender'] = 'male' # obj1.gender = 'male'
obj2.__dict__['name'] = 'zoe' # obj2.name = 'zoe'
obj2.__dict__['age'] = 22 # obj2.age = 22
obj2.__dict__['gender'] = 'female' # obj2.gender = 'female'
print(obj1.__dict__, obj2.__dict__) # {'name': 'tom', 'age': 18, 'gender': 'male'} {'name': 'zoe', 'age': 22, 'gender': 'female'}
print(obj1.name) # tom
print(obj2.name) # zoe
'''方案2:将冗余的添加步骤封装成函数'''
def init(obj, name, age, gender):
obj.name = name # obj.__dict__['name'] = name
obj.age = age # obj.__dict__['age'] = age
obj.gender = gender # obj.__dict__['gender'] = gender
init(obj1, 'tom', 18, 'male')
init(obj2, 'zoe', 22, 'female')
print(obj1.name) # tom
print(obj2.name) # zoe
'''方案3:将函数写到类中去'''
obj1 = Student(1, 2, 3)
obj2 = Student(4, 5, 6)
Student.set_info(obj1, 'tom', 18, 'male')
Student.set_info(obj2, 'zoe', 22, 'female')
print(obj1.name) # tom
print(obj2.name) # zoe
'''方案4:类中针对给对象创建独有数据的函数名'''
obj1 = Student('tom', 18, 'male')
obj2 = Student('zoe', 22, 'female')
print(obj1.__dict__) # {'name': 'tom', 'age': 18, 'gender': 'male'}
print(obj2.__dict__) # {'name': 'zoe', 'age': 22, 'gender': 'female'}
绑定方法
class Student:
school = '南开大学'
def __init__(self, name, age):
self.name = name
self.age = age
def func(self):
print('%s正在调用func方法' % self.name)
def index(self):
print('%s正在调用index方法' % self.name)
obj1 = Student('tom', 18)
print(obj1) # <__main__.Student object at 0x0000000002573CC0>
obj2 = Student('victor', 22)
# 类调用类中函数 有几个参数就需要传几个参数
Student.index(obj2) # victor正在调用index方法
# 对象调用类中函数,将当前调用的对象当做第一个参数自动传入
obj1.func() # tom正在调用func方法
obj2.func() # victor正在调用func方法
print(obj1.func) # <bound method Student.func of <__main__.Student object at 0x0000000002553D30>>
print(obj2.index) # <bound method Student.index of <__main__.Student object at 0x0000000002553F28>>