栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【python面向对象】类和对象(三)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【python面向对象】类和对象(三)

文章目录
  • 1.静态方法导入
  • 2.静态方法讲解
  • 3.练习
  • 4.类和对象总结(重要)

1.静态方法导入
"""
    静态方法引入
"""
list01 = [
    ["00","01","02","03"],
    ["10","11","12","13"],
    ["20","21","22","23"],
]
class Vector2:
    """
        二维向量
        可以表示位置/方向
    """
    def __init__(self,x, y):
        self.x = x
        self.y = y
#函数:便是左边方向
def left():
    return Vector2(0,-1)

#函数:便是右边方向
def right():
    return Vector2(0,1)

#作用:位置 + 方向
pos01 = Vector2(1,2)
l01 = left()
pos01.x += l01.x
pos01.y += l01.y
print(pos01.x, pos01.y)
#1 1
2.静态方法讲解

静态方法:将函数移到类中。

"""
    静态方法
"""
list01 = [
    ["00","01","02","03"],
    ["10","11","12","13"],
    ["20","21","22","23"],
]
class Vector2:
    """
        二维向量
        可以表示位置/方向
    """
    def __init__(self,x, y):
        self.x = x
        self.y = y
    #静态方法:便是左边方向
    @staticmethod
    def left():
        return Vector2(0,-1)

    @staticmethod
    #静态方法:便是右边方向
    def right():
        return Vector2(0,1)

#作用:位置 + 方向
pos01 = Vector2(1,2)
#通过类名调用静态方法
l01 = Vector2.left()
pos01.x += l01.x
pos01.y += l01.y
print(pos01.x, pos01.y)
#1 1

进阶:

"""
    静态方法
"""
list01 = [
    ["00","01","02","03"],
    ["10","11","12","13"],
    ["20","21","22","23"],
]
class Vector2:
    """
        二维向量
        可以表示位置/方向
    """
    def __init__(self,x, y):
        self.x = x
        self.y = y
    #静态方法:便是左边方向
    @staticmethod
    def left():
        return Vector2(0,-1)

    @staticmethod
    #静态方法:便是右边方向
    def right():
        return Vector2(0,1)

#作用:位置 + 方向
pos01 = Vector2(1,2)
#通过类名调用静态方法
# l01 = Vector2.left()
# pos01.x += l01.x
# pos01.y += l01.y
# print(pos01.x, pos01.y)
class DoubleListHelper:
    @staticmethod
#例如:list01 “10” 右边3 ->"11 ","12","13"
    def get_elements(target, vect_pos, vect_dir,count):
        """
            在二维列表中获取指定位置,指定方向,指定数量的元素。
        :param target: 二维列表
        :param vect_pos: 指定位置
        :param vect_dir: 指定方向
        :param count: 指定数量
        :return: 列表
        """
        list_result = []
        for i in range(count):
            vect_pos.x += vect_dir.x
            vect_pos.y += vect_dir.y
            element = target[vect_pos.x][vect_pos.y]
            list_result.append(element)
        return list_result


re = DoubleListHelper.get_elements(list01, Vector2(1,0), Vector2.right(),3)
print(re)
#['11', '12', '13']

#例如:list01"23" 左边 2
re = DoubleListHelper.get_elements(list01, Vector2(2,3), Vector2.left(),2)
print(re)
# ['22', '21']
3.练习

练习1:

list01 = [
    ["00","01","02","03"],
    ["10","11","12","13"],
    ["20","21","22","23"],
]
  • 在上面二维列表中,获取13位置,向左,3个元素
  • 在上面二维列表中,获取22位置,向上,2个元素
  • 在上面二维列表中﹐获取03位置,向下,2个元素
class Vector:
    def __init__(self,x,y):
        self.x = x
        self.y = y
    @staticmethod
    def left():
        return Vector(0,-1)
    @staticmethod
    def up():
        return Vector(-1,0)
    @staticmethod
    def down():
        return Vector(1,0)

list01 = [
    ["00", "01", "02", "03"],
    ["10", "11", "12", "13"],
    ["20", "21", "22", "23"],
]
list_result = []
class Move:
    @staticmethod
    def elements(target,vect_pos,vect_dir,count):
        for item in range(count):
            vect_pos.x += vect_dir.x
            vect_pos.y += vect_dir.y
            list_result.append(target[vect_pos.x][vect_pos.y])
        return list_result
#print(Move.elements(list01,Vector(1,3),Vector.left(),3))#['12', '11', '10']
#print(Move.elements(list01,Vector(2,2),Vector.up(),2))#['12', '02']
print(Move.elements(list01,Vector(0,3),Vector.down(),2))#['13', '23']

练习2:
定义敌人类

  • 数据:姓名,血量,基础攻击力,防御力
  • 行为:打印个人信息
  • 创建敌人列表(至少4个元素)。
  • 查找姓名是"灭霸"的敌人对象。
  • 查找所有死亡的敌人。
  • 计算所有敌人的平均攻击力。
  • 删除防御力小于10的敌人。
  • 将所有敌人攻击力增加50。
class Enemy:
    def __init__(self,name,health,attack,defence):
        self.name = name
        self.health = health
        self.attack = attack
        self.defence = defence
    def print_enemy_info(self):
        print("姓名是%s,血量是%d,基础攻击力是%d,防御力是%d"%(self.name,self.health,self.attack,self.defence))

list01 = [
    Enemy("张三", 0, 100, 20),
    Enemy("灭霸", 90, 60, 50),
    Enemy("李四", 10, 10, 4),
    Enemy("王五", 0, 80, 30),
]
def find01():
    for item in list01:
        if item.name == "灭霸":
            return item
    # return None
re = find01()
#如果没找到·返回值为None
#所以可以判断不是空﹐再调用其实例方法.
#if e01 != None:
if re:
    re.print_enemy_info()#姓名是灭霸,血量是90,基础攻击力是60,防御力是50
else:
    print("没找到")


def find02():
    list_result = []
    for item in list01:
        if item.health == 0:
            list_result.append(item)
    return list_result
re = find02()
for item in re:
    item.print_enemy_info()
#姓名是张三,血量是0,基础攻击力是100,防御力是20
#姓名是王五,血量是0,基础攻击力是80,防御力是30

def calculate01():
    count = 0
    for item in list01:
        count += item.attack
    return count / len(list01)
print(calculate01())#62.5

# def delete01():
#     #3 2 1 0
#     for item in range(len(list01)-1,-1,-1):
#         if list01[item].defence < 10:
#             del list01[item]
# #     return list01
# # re = delete01()
# delete01()
# for item in list01:
#     item.print_enemy_info()
# 姓名是张三,血量是0,基础攻击力是100,防御力是20
# 姓名是灭霸,血量是90,基础攻击力是60,防御力是50
# 姓名是王五,血量是0,基础攻击力是80,防御力是30

print("----------------------------------")

def increse_attack():
    for item in list01:
        item.attack += 50
increse_attack()
for item in list01:
    item.print_enemy_info()
# 姓名是张三,血量是0,基础攻击力是150,防御力是20
# 姓名是灭霸,血量是90,基础攻击力是110,防御力是50
# 姓名是李四,血量是10,基础攻击力是60,防御力是4
# 姓名是王五,血量是0,基础攻击力是130,防御力是30
4.类和对象总结(重要)
  • 类:抽象 向量 class Vector2 str int list

  • 对象:具体 (1,2) Vector(1,2) "a" 1 [1,2]

  • 类和对象区别:类和类行为不同,对象和对象数据不同

  • Vector2(1,2) Vector(3,4) :同一个类型的多个对象,数据不同(1,2/3,4),行为(求方向,求大小)相同.

类成员:

  • 实例:对象的数据(变量),对象的行为(方法)。
  • 类:类的数据(变量),类的行为(方法)。可以被所有对象共同操作的数据。
  • 静态:即不需要操作实例变量也不需要操作类变量。
  • 实例方法操作实例变量,表示"个体"行为。
  • 类方法操作类变量,表示"大家"行为。
  • 静态方法不能操作数据,表示为函数都可以。
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/850576.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号