编程思想及对象与类

2022/7/27 14:24:07

本文主要是介绍编程思想及对象与类,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

  • 目录
    • 编程思想
      • 面向对象
      • 面向过程  
    • 对象与类的概念
    • 对象与类的创建
    • 对象的实例化方法-独有数据
  • 编程思想

   1.面向对象

    1.1. 面向对象前戏

案例:人狗大战

# 需求:人狗大战
# 1.'创造'出人和狗
步骤1: 模拟一个个人和狗
# person1 = { 
# 'name': 'jason',
# 'attack_val': 800,
# 'life_val': 2000
# }
# person2 = {
# 'name': 'kevin',
# 'attack_val': 100,
# 'life_val': 1200
# }
# dog1 = { 
# 'name': '小黑狗',
# 'd_type': '泰迪',
# 'attack_val': 50,
# 'life_val': 800
# }
# dog2 = { 
# 'name': '小花狗',
# 'd_type': '哈士奇',
# 'attack_val': 800,
# 'life_val': 8000
# }

# 步骤二 封装函数:存在的问题数据与功能之间互相调用
# 面向对象:将特定的数据与特定的功能绑定

def get_person(name, gender, age, p_type, attack_val, life_val):
person_obj = {
'name': name,
'gender': gender,
'age': age,
'p_type': p_type,
'attack_val': attack_val,
'life_val': life_val
}
return person_obj



# p1 = get_person('jason', 'male', 18, 8000, 80000)
# p2 = get_person('kevin', 'female', 28, 10, 80)
def get_dog(name, d_type, attack_val, life_val):
dog_obj = {
'name': name,
'd_type': d_type,
'attack_val': attack_val,
'life_val': life_val
}
return dog_obj



# d1 = get_dog('小黄狗','泰迪',800, 5000)
# d2 = get_dog('小花狗','哈士奇',100, 200)

步骤三:将特定的数据与特定功能绑定

def person_attack(person_obj, dog_obj):
print('即将被攻击的狗:%s 当前血量:%s' % (dog_obj.get('name'), dog_obj.get('life_val'))) # 先展示当前狗的状态
dog_obj['life_val'] -= person_obj.get('attack_val') # 人锤狗 直接用狗的生命值减去人的攻击力
print('人:%s 锤了 狗:%s 狗掉血:%s 剩余血量:%s' % (
person_obj.get('name'), dog_obj.get('name'), person_obj.get('attack_val'), dog_obj.get('life_val')))



def dog_attack(dog_obj, person_obj):
print('即将被攻击的人:%s 当前血量:%s' % (person_obj.get('name'), person_obj.get('life_val'))) # 先展示当前人的状态
person_obj['life_val'] -= dog_obj.get('attack_val') # 狗咬人 直接用人的生命值减去狗的攻击力
print('狗:%s 咬了 人:%s 人掉血:%s 剩余血量:%s' % (
dog_obj.get('name'), person_obj.get('name'), dog_obj.get('attack_val'), person_obj.get('life_val')))



# 调用产生人和狗的函数
p1 = get_person('jason', 'male', 18, '猛男', 8000, 90000)
p2 = get_person('kevin', 'female', 28, '淑女', 10, 200)
d1 = get_dog('小黄狗', '恶霸犬', 800, 8000)
d2 = get_dog('小黑狗', '巴哥犬', 200, 500)
# 调用攻击彼此的函数
# person_attack(p1, d1)
dog_attack(d2, p2)

 

    1.2 定义:将特定的数据与特定的功能相绑定   

   2.面向过程

    2.1.定义:按照流程达到一些列想要实现的结果

   3.面向对象与面向过程的编程思想没有优劣之分,多数情况下是共同存在

  • 对象与类的概念

   1.对象的概念:将特定数据与功能相结合的存储容器

   2.类的概念:将多个对象具有的相同数据与功能结合体的存储容器

    2.1 类的作用:节省代码(先有类才有对象)

   3.区分:

    3.1 对象是存储不同数据与功能的结合体

    3.2 类是存储相同数据与功能的对象的结合体

  • 对象与类的创建

   1.类的语法:

    1.1 关键字:class 

    1.2 类名 :类名与变量名相似,采取'大驼峰'的形式命名

    1.3 类体代码(在定义阶段就会执行-与函数的区别):共同的数据与方法

 

   2.类与对象的创建:类名点__dict__(数据类型是字典)

class Computer:
    name = '电脑'        # 数据

    def use_computer(self):
        print(f'{self}在使用电脑')


print(Computer.__dict__)             # 字典类型    查看名称空间
print(Computer.__dict__['name'])     # 电脑
print(Computer.name)                 # 电脑
print(Computer.__dict__.get('use_computer'))    # <function Computer.use_computer at 0x000001FD71945288>   
Computer.use_computer('students')    # students在使用电脑

   3.对应键值对的值修改:类名.字典k键 = 字典v值

class Computer:
    name = '电脑'        # 数据

    def use_computer(self):
        print(f'{self}在使用电脑')

Computer.name = '键盘'  # 改值    等同于Computer.__dict__.['name']
 print(Computer.name) # 键盘
  • 对象的实例化方法-独有的数据

   1.实例化:类名加括号

    1.1 新增键值对

class Computer:
    name = '电脑'        # 数据

    def use_computer(self):
        print(f'{self}在使用电脑')
com1 = Computer()
com2 = Computer()
print(com1.name)    # 电脑
print(com2.name)    # 电脑
print(com1)         # <__main__.Computer object at 0x000002097DC7AE48>
print(com2)         # <__main__.Computer object at 0x000002097DC7EA88>
print(com1.__dict__)   # {}
print(com2.__dict__)   # {}
class Computer:
    name = '电脑'        # 数据

    def use_computer(self):
        print(f'{self}在使用电脑')
com1 = Computer()
com1.__dict__['type'] = '联想'
print(com1.__dict__)      # {'type': '联想'}
print(com1.type)          # 联想

    1.2 将新增键值对封装成函数

class Computer:
    name = '电脑'        # 数据

    def use_computer(self):
        print(f'{self}在使用电脑')
com1 = Computer()
2. 将新增键值对封装成函数
def init(com1, type, brand):
    com1.__dict__['type'] = type
    com1.__dict__['brand'] = brand

init(com1, '笔记本', '联想')
print(com1.__dict__)      # {'type': '笔记本', 'brand': '联想'}

    1.3 将函数封装到类中

3 封装函数
class Computer:

    def __init__(self, type, brand):
        self.type = type         # 等于与com1.__dict__['type'] = type
        self.brand = brand       # com1.__dict__['brand'] = brand
        name = '电脑'        # 数据

        def use_computer(self):
            print(f'{self}在使用电脑')


com1 = Computer('笔记本', '联想')      # {'type': '笔记本', 'brand': '联想'}
print(com1.type)                     # 笔记本

 



这篇关于编程思想及对象与类的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程