美文网首页
P 面向对象 | 面向对象的封装特性

P 面向对象 | 面向对象的封装特性

作者: Ricsy | 来源:发表于2019-10-04 16:20 被阅读0次


    名称 描述
    封装 根据职责将对象的属性和方法封装到一个抽象的类中
    继承 实现代码的重用,相同的代码不需要重复的编写
    多态 不同的子类对象调用相同的父类方法,产生不同的执行结果,增加代码的灵活度

    一、封装

    将属性和方法封装到一个抽象的类中
    外界使用类创建对象,然后让对象调用方法
    对象方法的细节都被封装在类的内部

    • 在对象的方法内部,可以直接访问对象的属性
      self.属性名
    • 同一类创建多个对象之间,属性互不干扰
      见后面的示例——小美也爱跑步
    • 一个对象的属性可以是另外一个类创建的对象
      见后面的示例——士兵突击

    eg:


    • 小明爱跑步的需求

    提炼名词和动词

    1. 小明爱跑步
    class Person:
        def __init__(self, name, weight):
            self.name = name
            self.weight = weight
    
        def __str__(self):
            return '我的名字叫%s,体重是%.2f' % (self.name, self.weight)
    
        def run(self):
            print("%s爱跑步,跑步锻炼身体" % self.name)
            self.weight -= 0.5
    
        def eat(self):
            print("%s是吃货,吃完这顿再减肥" % self.name)
            self.weight += 1
    
    
    if __name__ == '__main__':
        person = Person('小明', 75)
        print(person)
        person.run()
        print(person)
        person.eat()
        print(person)
    
    
    1. 小明爱跑步,小美也爱跑步
    class Person:
        def __init__(self, name, weight):
            self.name = name
            self.weight = weight
    
        def __str__(self):
            return '我的名字叫%s,体重是%.2f' % (self.name, self.weight)
    
        def run(self):
            print("%s爱跑步,跑步锻炼身体" % self.name)
            self.weight -= 0.5
    
        def eat(self):
            print("%s是吃货,吃完这顿再减肥" % self.name)
            self.weight += 1
    
    
    if __name__ == '__main__':
        person = Person('小明', 75)
        person1 = Person('小美', 45)
        print(person)
        person.run()
        print(person)
        person.eat()
        print(person)
        print('-'*50)
        print(person1)
        person1.run()
        print(person1)
        person1.eat()
        print(person1)
    
    

    • 摆放家具的需求
    1. 摆放家具
    class HouseItem:
        def __init__(self, name, area):
            self.name = name
            self.area = area
    
        def __str__(self):
            return "[%s]占地%.2f" % (self.name, self.area)
    
    
    class House:
        def __init__(self, house_type, area):
            self.house_type = house_type
            self.area = area
            self.free_area = area
            self.item_list = []
    
        def __str__(self):
            # Python能够自动将()内的代码连接在一起
            return ("户型:%s,总面积:%.2f,[剩余面积:%.2f],家具:%s"
                    % (self.house_type, self.area,
                       self.free_area, self.item_list))
    
        def add_item(self, item):
            print("要添加%s" % item)
            if self.free_area <= item.area:
                print("家具面积太大了,无法添加")
                return
            self.item_list.append(item.name)
            self.free_area -= item.area
    
    
    if __name__ == '__main__':
        bed = HouseItem('席梦思', 4)
        chest = HouseItem('衣柜', 2)
        table = HouseItem('餐桌', 1.5)
        house = House('两室一厅', 40)
        print(house)
        print('-' * 50)
        house.add_item(bed)
        house.add_item(chest)
        house.add_item(table)
        print(house)
    
    
    • 士兵突击

    一个对象的属性可以是另外一个类创建的对象

    class Gun:
        def __init__(self, module):
            self.module = module
            self.bullet_count = 0
    
        def add_bullet(self, count):
            self.bullet_count += count
    
        def shoot(self):
            if self.bullet_count <= 0:
                print("[%s]没有子弹了......" % self.module)
                return
            self.bullet_count -= 1
            print("[%s]突突突...剩余子弹数量:%-2d " % (self.module, self.bullet_count))
    
    
    class Soldier:
        def __init__(self, name):
            self.name = name
            self.gun = None
    
        def fire(self):
            # 判断士兵是否有枪
            if self.gun is None:
                print("%s没有枪" % self.name)
                return
            print("%s: 冲啊......" % self.name)
            ak47.add_bullet(10)
            ak47.shoot()
    
    
    if __name__ == '__main__':
        ak47 = Gun('AK47')
        xusanduo = Soldier('许三多')
        xusanduo.gun = ak47
        xusanduo.fire()
        print(xusanduo.gun)
    
    

    更新中......


    相关文章

      网友评论

          本文标题:P 面向对象 | 面向对象的封装特性

          本文链接:https://www.haomeiwen.com/subject/vuwjpctx.html