ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

面向对象

2022-07-26 20:04:37  阅读:105  来源: 互联网

标签:death name Butcher 面向对象 sands print Desert


面向对象

面向对象前戏

1.案例(沙漠死神内瑟斯大战荒漠屠夫雷克顿)
# 1.首先创建出沙漠死神内瑟斯和荒漠屠夫雷克顿
# Desert_of_death = { # 沙漠死神构建
#     'name':'inside',
#     'death_type': 'soaring',
#     'the_soul_from': 300,
#     'health_point': 4000
# }
# The_Butcher_of_the_sands = {  # 荒漠屠夫构建
#     'name': 'renekton',
#     'sands':'soaring',
#     'the_tyrant_beat': 350,
#     'health_point': 3500
# }


# 2.封装起来反复使用
def Get_Desert_of_death(name, death_type, attack, health_point):
    """
    # 首先创建出沙漠死神的用户字典
    :param name:姓名
    :param death_type:类型
    :param attack:攻击方式
    :param health_point:生命值
    :return:沙漠死神的字典
    """

    Desert_of_death = {  # 沙漠死神构建
        'name': name,
        'death_type': death_type,
        'the_soul_from': attack,
        'health_point': health_point
    }
    return Desert_of_death


# p1 = get_Desert_of_death('inside', 'soaring', 300, 4000)
def Get_The_Butcher_of_the_sands(name, death_type, attack, health_point):
    """
    # 首先创建荒漠屠夫的用户字典
    :param name: 姓名
    :param death_type:类型 
    :param attack: 攻击方式
    :param health_point: 生命值
    :return: 荒漠屠夫的字典
    """
    The_Butcher_of_the_sands = {  # 荒漠屠夫构建
        'name': name,
        'sands': death_type,
        'the_tyrant_beat': attack,
        'health_point': health_point
    }
    return The_Butcher_of_the_sands


# p2 = get_The_Butcher_of_the_sands('renekton', 'soaring', 350, 3500)
# 3.让沙漠死神内瑟斯和荒漠屠夫雷克顿大战起来
def Desert_of_death_attack(Desert_of_death, The_Butcher_of_the_sands):
    """
    内瑟斯打雷克顿
    :param Desert_of_death:传内瑟斯数据
    :param The_Butcher_of_the_sands:雷克顿数据
    :return:
    """
    print('即将被攻击的:%s, 当前血量 %s' % (The_Butcher_of_the_sands.get('name'),
                                  The_Butcher_of_the_sands.get('health_point')))
    The_Butcher_of_the_sands['health_point'] -= Desert_of_death['the_soul_from']
    print('Desert_of_death:%s 使用 the_soul_from 攻击 The_Butcher_of_the_sands:%s ,The_Butcher_of_the_sands 掉血:%s 剩余血量:%s' %
          (Desert_of_death.get('name'), The_Butcher_of_the_sands.get('name'), Desert_of_death.get('the_soul_from'),
        The_Butcher_of_the_sands.get('health_point')))


def The_Butcher_of_the_sands_attack(The_Butcher_of_the_sands, Desert_of_death):
    """
    雷克顿打内瑟斯
    :param The_Butcher_of_the_sands:传内瑟斯数据
    :param Desert_of_death:雷克顿数据
    :return:
    """
    print('即将被攻击的:%s, 当前血量 %s' % (Desert_of_death.get('name'),
                                  Desert_of_death.get('health_point')))
    Desert_of_death['health_point'] -= The_Butcher_of_the_sands['the_soul_from']
    print('Desert_of_death:%s 使用 the_tyrant_beat 攻击 The_Butcher_of_the_sands:%s ,'
          ' The_Butcher_of_the_sands 掉血:%s 剩余血量:%s' % (The_Butcher_of_the_sands.get('name'),
          Desert_of_death.get('name'), The_Butcher_of_the_sands.get('the_tyrant_beat'),
          Desert_of_death.get('health_point')))


# 生产内瑟斯和雷克顿数据
p1 = Get_Desert_of_death('inside', 'soaring', 300, 4000)
p2 = Get_The_Butcher_of_the_sands('renekton', 'soaring', 350, 3500)
# 开打
Desert_of_death_attack(p1, p2)  
"""
即将被攻击的:renekton, 当前血量 3500
Desert_of_death:inside 使用 the_soul_from 攻击 The_Butcher_of_the_sands:renekton ,The_Butcher_of_the_sands 掉血:300 剩余血量:3200
"""

image

优化代码

def Get_Desert_of_death(name, death_type, attack, health_point):
    """
    # 首先创建出沙漠死神的用户字典
    :param name:姓名
    :param death_type:类型
    :param attack:攻击方式
    :param health_point:生命值
    :return:沙漠死神的字典
    """

    def Desert_of_death_attack(Desert_of_death, The_Butcher_of_the_sands):
        """
        内瑟斯打雷克顿
        :param Desert_of_death:传内瑟斯数据
        :param The_Butcher_of_the_sands:雷克顿数据
        :return:
        """
        print('即将被攻击的:%s, 当前血量 %s' % (The_Butcher_of_the_sands.get('name'),
                                      The_Butcher_of_the_sands.get('health_point')))
        The_Butcher_of_the_sands['health_point'] -= Desert_of_death['the_soul_from']
        print(
            'Desert_of_death:%s 使用 the_soul_from 攻击 The_Butcher_of_the_sands:%s ,The_Butcher_of_the_sands 掉血:%s 剩余血量:%s' %
            (Desert_of_death.get('name'), The_Butcher_of_the_sands.get('name'), Desert_of_death.get('the_soul_from'),
             The_Butcher_of_the_sands.get('health_point')))


    Desert_of_death = {  # 沙漠死神构建
        'name': name,
        'death_type': death_type,
        'the_soul_from': attack,
        'health_point': health_point,
        'Desert_of_death_attack':Desert_of_death_attack
    }
    return Desert_of_death


def Get_The_Butcher_of_the_sands(name, death_type, attack, health_point):
    """
    # 首先创建荒漠屠夫的用户字典
    :param name: 姓名
    :param death_type:类型
    :param attack: 攻击方式
    :param health_point: 生命值
    :return: 荒漠屠夫的字典
    """

    def The_Butcher_of_the_sands_attack(The_Butcher_of_the_sands, Desert_of_death):
        """
        雷克顿打内瑟斯
        :param The_Butcher_of_the_sands:传内瑟斯数据
        :param Desert_of_death:雷克顿数据
        :return:
        """
        print('即将被攻击的:%s, 当前血量 %s' % (Desert_of_death.get('name'),
                                      Desert_of_death.get('health_point')))
        Desert_of_death['health_point'] -= The_Butcher_of_the_sands['the_soul_from']
        print('Desert_of_death:%s 使用 the_tyrant_beat 攻击 The_Butcher_of_the_sands:%s ,'
              ' The_Butcher_of_the_sands 掉血:%s 剩余血量:%s' % (The_Butcher_of_the_sands.get('name'),
                Desert_of_death.get('name'),The_Butcher_of_the_sands.get('the_tyrant_beat'),Desert_of_death.get('health_point')))

    The_Butcher_of_the_sands = {  # 荒漠屠夫构建
        'name': name,
        'sands': death_type,
        'the_tyrant_beat': attack,
        'health_point': health_point,
        'The_Butcher_of_the_sands_attack':The_Butcher_of_the_sands_attack
    }
    return The_Butcher_of_the_sands


# p2 = get_The_Butcher_of_the_sands('renekton', 'soaring', 350, 3500)
# 3.让沙漠死神内瑟斯和荒漠屠夫雷克顿大战起来

# 生产内瑟斯和雷克顿数据
p1 = Get_Desert_of_death('inside', 'soaring', 300, 4000)
p2 = Get_The_Butcher_of_the_sands('renekton', 'soaring', 350, 3500)
print(p1, p2)
p1.get('Desert_of_death_attack')(p1, p2)
"""
1.将文件的数据与功能绑定到一起,这样就不会出现互相调用混乱的情况,将来也只能在自己的函数中使用自己的值这种方式我们称为面向对象。
"""

image

两种编程思想

1.面向过程编程
	我们之前写的代码通常都是面向过程的编程,我们只要将过程写出来那么就可以得到最后的结果。
2.面向对象编程
	面向对象编程,对象其实就是一个容器,该容器可以盛放数据与功能,,所以我们可以说对象其实就是将数据和功能整合到一起的产物,或者说对象就是一个盛放东西的容器。
面向对象就是要造出一个个的对象,将原本分开的散数据和功能整合到一个个的对象里,这么做既方便使用,也可以提高程序的耦合度,进而提升了程序的可扩展性。就像我们的游戏人物程序员只能管将他造出来,并不能控制她之后的操作等。
3.两种编程思想没有好坏之分,只是应用场景不一样,大多时候还是两种思想混合使用更为合适。

image

对象与类型的概念

1.类其实就是类别和种类,就像代英帝国分而治之激化种族矛盾使用的就是将人种分而治之,各种人都不服另一种人之间资源不匹配,就会将矛盾由外转向内部矛盾,话外题。类就是面向对象分析和设计的即使,如果多个对象有相似的数据和功能,那么该多个对象就属于同一类型,有了类的好处就是,我们可以将同一类的对象相同的数据与功能存放到类里,而无需每个对象都保存一份,这样每个对象里只需存自己独有的数据即可,极大地节省了内存空间,所以如果说对象是用来存放数据与功能的容器的话,那么类则是存放多个相同的数据与功能的容器。
2.在程序中,必须要实现定义类,然后再调用类产生对象,产生的对象与对象之间存在关联,这种关联指的是:对象可以访问到类中共有的数据与功能,所以类中的内容仍然是属于对象的,类只不过是一种节省空间、减少代码冗余的机制,面向对象程序最终的核心仍然是使用对象。

image

对象与类的创建

1.在我们现实生活中,我们一般对一个人称之为个体,一群人则就会称之为群体也就是类,就像MAGA,但是再编程中我们必须要先有类才可以产生对象。
2.面向对象编程本质上就是将数据和功能绑定到一起,但是为了突出面向对象编程的形式,python就专门开发了一套语法专门去用于面向对象操作。
3.类的完整语法
    class 类名:
        # 类中对象的公共数据
        # 类中对象的公共方法
	3.1class是定义类的关键字
	3.2类名和变量名函数名一样都需要做到见名知意并且建议首字母大写
	3.3类体代码,也就是公共的数据和方法类体代码再定义阶段就会被执行
4.查看名称空间方法
class Student: # 类的命名应该使用首字母大写

    school='清华大学' # 数据

    def choice(self): # 功能
        print('choosing a course')  # 选课

print(Student.__dict__)  # 使用该方法查看名称空间 可以看成是一个字典
print(Student.__dict__['school'])  # 使用字典的取值方式获取名字  # 清华大学
print(Student.__dict__.get('choice'))  # 使用字典的取值方式获取名字
'''在面向对象编程中 想要获取名称空间中的名字 可以采用句点符'''
print(Student.school)  # 清华大学
print(Student.choice)  # <function Student.choice at 0x0000022915FDF820>
'''类实例化产生对象>>>: 类名加括号'''
stu1 = Student()  # 清华大学
stu2 = Student()  # <function Student.choice at 0x0000022915FDF820>
print(stu1.school)  # 清华大学
print(stu2.school)  # 清华大学
print(stu1)  # <__main__.Student object at 0x000001D923B04A60>
print(stu2)  # <__main__.Student object at 0x0000025E8A48F130>
print(stu1.__dict__, stu2.__dict__)  # {} {}
print(stu1.school)  # 清华大学
print(stu2.school)  # 清华大学
print(stu1.choice)  # <bound method Student.choice of <__main__.Student object at 0x0000022915EB2E80>>
print(stu2.choice)  # <bound method Student.choice of <__main__.Student object at 0x0000022915EB2E80>>
Student.school = '北京大学'  # 修改school键对应的值
print(stu1.school)  # 北京大学
print(stu2.school)  # 北京大学
"""
我们习惯将类或者对象句点符后面的东西称为属性名或者方法名
"""

image

对象的独有数据

1.方法一:
class Student:
    school = '中国传媒大学'
    def choice_course(self):
        print('甄别课程')
obj1 = Student()
obj1.__dict__['name'] = 'joseph'
obj1.__dict__['age'] = 21
obj1.__dict__['hobby'] = 'read'
print(obj1.name)  # joseph
print(obj1.age)  # 21
print(obj1.hobby)  # read
print(obj1.school)  # 中国传媒大学
obj2 = Student()
obj2.__dict__['name'] = 'trump'
obj2.__dict__['age'] = 76
obj2.__dict__['hobby'] = 'golf'
print(obj2.name)  # trump
print(obj2.age)  # 76
print(obj2.hobby)  # golf

2.方法二:
def init(obj, name, age, hobby):
    obj.__dict__['name'] = name
    obj.__dict__['age'] = age
    obj.__dict__['hobby'] = hobby
Stu1 = Student()
Stu2 = Student()
init(Stu1, 'joseph', 21, 'read')
init(Stu2, 'Trump', 76, 'golf')
print(Stu1.__dict__)  # {'name': 'joseph', 'age': 21, 'hobby': 'read'}
print(Stu2.__dict__)  # {'name': 'Trump', 'age': 76, 'hobby': 'golf'}

3.方法三
class Student:
    def __init__(self, name, age, hobby):
        self.name = name  # obj.__dict__['name'] = name
        self.age = age  # obj.__dict__['age'] = age
        self.hobby = hobby  # obj.__dict__['hobby'] = hobby
    school = '中国传媒大学'
    def choice_course(self):
        print('选课')
stu1 = Student()
print(stu1.__dict__)
Student.__init__(stu1, 'joseph', 21, 'read')
print(stu1.__dict__)
print(stu1.name)

stu1 = Student('joseph', 21, 'read')
print(stu1)
stu2 = Student('Trump', 76, 'golf')
print(stu2)

image

标签:death,name,Butcher,面向对象,sands,print,Desert
来源: https://www.cnblogs.com/joseph-bright/p/16522460.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有