zoukankan      html  css  js  c++  java
  • Python全栈之路系列----之-----面向对象2(命名空间/组合)

    面向对象命名空间

    命名空间

    创建一个类就会创建一个类的民称空间,用来存储定义在类中的所有名字这些名字称之为类的属性

    而类有两种属性:静态属性和动态属性

      类的数据属性是共享给所有对象的  类的动态属性是绑定到所有对象的

    • 静态属性就是直接在类中定义的变量

      对于类的静态属性:
                   如果类.属性 调用的就是类中的属对象.属性  先从自己的内存空间里找名字 找到了用自的 

                没找到用类的,如果类中也没 有就报错

    • 动态属性就是定义在类中的方法

        关于类的动态属性(方法):
             这个方法本身就存在类中,并不会存在对象的内存中
             但是在对象调用类中的方法的时候 要依赖于一个地址簿去类中寻找对应的方法

    • 关于对象的属性
          对象的属性就存在对象的命名空间中
            只能被对象调用、修改
            不能被类调用

    命名空间代码理解

    class A:
        country = '印度'
        def show_name(self):
            print(self.name)
    A.sex = 'Male'
    a = A()
    a.name = 'alex'
    a.show_name()
    
    # 例一
    # class A:
    #     country='印度'
    #     def show_name(self):
    #         print(self.name)
    #
    # a=A() #实例化对象
    # a.name='alex'  #给对象创建了一个naem属性  本质就是在init里面创建的
    # a.show_name()   #调用了showname的方法 打印一个alex
    
    #例二
    class A:
        country='印度'
        def show_name(self):
            print(self.name)
    a=A()
    # a.name = 'alex'
    a.show_name='egon'
    # print(a.show_name)
    a.show_name() #报错
    
    # 例三
    class A:
        country='印度'
        def show_name(self):
            print(self.name)
    
    a = A() #实例的对象
    b = A() #实例的对象
    print(A.country)
    print(a.country) #首先先找到了a对象的内存  再找A的内存
    print(b.country)
    
    a.country='中国' #给a的对象的内存里创建了一个属性
    print(A.country)    #印度
    print(a.country)    #中国
    print(b.country)    #印度

    类的组合引用

    组合指的是,在一个类中以另外一个类的实例化对象作为数据属性,称为类的组合

    class Weapon:
        def prick(self, obj):  # 这是该装备的主动技能,扎死对方
            obj.life_value -= 500  # 假设攻击力是500
    
    class Person:  # 定义一个人类
        role = 'person'  # 人的角色属性都是人
    
        def __init__(self, name):
            self.name = name  # 每一个角色都有自己的昵称;
            self.weapon = Weapon()  # 给角色绑定一个武器;
            
    egg = Person('egon')
    egg.weapon.prick() 
    #egg组合了一个武器的对象,可以直接egg.weapon来使用组合类中的所有方法
    • 组合表达的是 什么有什么的 一种关系
    • 组合增强了代码的重用性

    圆环是由两个圆组成的,圆环的面积是外面圆的面积减去内部圆的面积。圆环的周长是内部圆的周长加上外部圆的周长。
    这个时候,我们就首先实现一个圆形类,计算一个圆的周长和面积。然后在"环形类"中组合圆形的实例作为自己的属性来用

    from math import pi
    
    class Circle:
        '''
        定义了一个圆形类;
        提供计算面积(area)和周长(perimeter)的方法
        '''
        def __init__(self,radius):
            self.radius = radius
    
        def area(self):
             return pi * self.radius * self.radius
    
        def perimeter(self):
            return 2 * pi *self.radius
    
    
    circle =  Circle(10) #实例化一个圆
    area1 = circle.area() #计算圆面积
    per1 = circle.perimeter() #计算圆周长
    print(area1,per1) #打印圆面积和周长
    
    class Ring:
        '''
        定义了一个圆环类
        提供圆环的面积和周长的方法
        '''
        def __init__(self,radius_outside,radius_inside):
            self.outsid_circle = Circle(radius_outside)
            self.inside_circle = Circle(radius_inside)
    
        def area(self):
            return self.outsid_circle.area() - self.inside_circle.area()
    
        def perimeter(self):
            return  self.outsid_circle.perimeter() + self.inside_circle.perimeter()
    
    
    ring = Ring(10,5) #实例化一个环形
    print(ring.perimeter()) #计算环形的周长
    print(ring.area()) #计算环形的面积
    圆环

    用组合的方式建立了类与组合的类之间的关系,它是一种‘有’的关系,比如教授有生日,教授教python课程

    class Birthday:
        def __init__(self,year,month,day):
            self.year = year
            self.month = month
            self.day = day
    
    #课程类
        #课程的名字
        #课程周期
        #授课的老师
        #课程的价格
    class Course:
        def __init__(self,name,period,teacher,price):
            self.name=name
            self.period=period
            self.teacher=teacher
            self.price=price
    
    class Person:
        def __init__(self,name,birth_obj,kecheng):
            self.name = name
            self.birthday = birth_obj
            self.kecheng=kecheng
    
    bith_hai = Birthday(1988,11,11)
    py=Course('python',20,'jingdao',20000)
    haijiao = Person('海娇',bith_hai,py)
    print(haijiao.name)
    print(haijiao.birthday.year)
    print(haijiao.birthday.month)
    print(haijiao.kecheng.name)
    
    
    class Birthday:
        def __init__(self,year,month,day):
            self.year = year
            self.month = month
            self.day = day
    
    class Course:
        def __init__(self,name,period,teacher,price):
            self.name=name
            self.period=period
            self.teacher=teacher
            self.price=price
    
    class Person:
        def __init__(self,name,birth_obj):
            self.name = name
            self.birthday = birth_obj
    
    bith_hai = Birthday(1988,11,11)
    py=Course('python',20,'jingdao',20000)
    haijiao = Person('海娇',bith_hai)
    print(haijiao.name)
    print(haijiao.birthday.year)
    print(haijiao.birthday.month)
    haijiao.kecheng = py
    print(haijiao.kecheng.name)
    
    例二

    当类之间有显著不同,并且较小的类是较大的类所需要的组件时,用组合比较好

    class Person:
        def __init__(self,name,sex,aggr,blood):
            self.name = name
            self.sex = sex
            self.aggr = aggr
            self.blood = blood
            self.money = 0
    
        def attack(self,dog):
            dog.blood -= self.aggr
    
        def equip(self,weapon):
            self.money -= weapon.price
            self.weapon = weapon
    
        def use_weapon(self,dog):
            self.weapon.hurt(dog)
            self.blood += self.weapon.back_blood
    
    class Dog:
        def __init__(self,name,kind,aggr,blood):
            self.name = name
            self.kind = kind
            self.aggr = aggr
            self.blood = blood
    
        def bite(self,person):
            person.blood -= self.aggr
    
    class Weapon:
        def __init__(self,name,attack,price,back_blood):
            self.name = name
            self.attack = attack
            self.price = price
            self.back_blood = back_blood
    
        def hurt(self,dog):
            dog.blood -= self.attack
    
    # egon = Person('egon','不详',250,380)
    egon = Person()
    alex = Person('alex','不详',20,40000)
    dog = Dog('egg','藏獒',500,20000)
    #加武器
    毒包子 = Weapon('毒包子',10000,200,300)
    egon.money = 200
  • 相关阅读:
    Android——Activity去除标题栏和状态栏
    Android——程序员的情怀——优化BaseAdapter
    Android——Android Sutido:[2]导入eclipse项目篇
    【Android开源项目分析】自定义圆形头像CircleImageView的使用和源码分析
    学佛略要
    Keystone, Start, Failed to Load Bson
    又梦见了你
    伦敦之旅
    无题
    Multiverse in Doctor Strange // Multiverse在《神秘博士》
  • 原文地址:https://www.cnblogs.com/zgd1234/p/7527458.html
Copyright © 2011-2022 走看看