zoukankan      html  css  js  c++  java
  • 面向对象--命名空间和组合

    一.命名空间

    1.类和对象的关系

    1.1在实例化时会创建一个空的命名空间,这个空间存放着对象的属性。如self.name self.age等
    1.2对象和类之间存在一个关联关系
    所以对象能够找到类
    但是 类不能找到对象
    1.3使用类名.属性 只会寻找类中的静态变量名字
    使用对象.属性 会现在对象自己的命名空间中找名字。如果找不到 再到类的内存空间中去找
    class Person:
        country="中国人"
    print(Person.country) #中国人
    alex=Person()   #创建空的命名空间
    egon=Person()
    alex.name="alex"
    egon.name="egon"
    alex.country="印度人"  #对象alex在自己的空间里创建了country=“印度人”这个属性
    print(alex.country)  #印度人  对象调用类中的属性 可行
    print(egon.country)  #中国人。这里的country是类中的属性
    #print(Person.name)  #AttributeError  类调用对象中的属性 不可行
    View Code

    2.只要使用静态变量 就用类名去调用。在类中可以不写init方法

    对象.money=1000 在对象的内存中会生成一个新的变量
    对象.money[index]=1000 不会在对象的内存中生成一个新的变量
    class Person:
        money=0
    mother=Person()
    mother.money+=1000 #在mother对象中建了money这个属性
    father=Person()
    father.money+=1000
    print(mother.money)  #1000
    print(father.money)  #1000
    print(mother.__dict__)   #{'money': 1000}
    print(Person.money)  #0
    
    class Person:
        money=[0]
    mother=Person()
    mother.money[0]+=1000
    father=Person()
    father.money[0]+=1000
    print(mother.money)  #[2000]
    print(father.money)  #[2000]
    print(mother.__dict__) #{} 在mother对象中没建money这个属性
    print(Person.money)  #[2000]
    View Code

    实例:写一个类,统计这个类被多少个对象实例化

    class Person:
        count = 0
        def __init__(self):
            Person.count += 1
    alex=Person()
    egon=Person()
    print(Person.count)
    View Code

    二.组合:两个类

    1.组合:一个类的对象属性是另一个类的对象
    2.两个类 :类与类之间有一种"什么有什么的关系"

    求圆环的面积和周长
    from math import pi
    class Circle:   #圆的面积和周长
        def __init__(self,r):
            self.r = r
        def area(self):
            return pi*(self.r**2)
        def perimeter(self):
            return 2*pi*self.r
    
    class Ring:  # 圆环的面积和周长
        def __init__(self, outter,inner):
            self.outter = Circle(outter)
            self.inner=Circle(inner)
        def area(self):
            return self.outter.area() - self.inner.area()
        def perimeter(self):
            return self.outter.perimeter() -self.inner.perimeter()
    cir=Ring(10,5)
    print(cir.area())
    print(cir.perimeter())
    View Code
    老师和生日的组合
    class Teacher:
        def __init__(self,name,sex,course,birth):
            self.name=name
            self.sex=sex
            self.course=course
            self.birth=birth
    class Birth:
        def __init__(self,year,mouth,day):
            self.year=year
            self.mouth=mouth
            self.day=day
    birth=Birth(1972,3,6)
    alex=Teacher("alex","man","math",birth)
    print(alex.birth.year)
    import time
    time1=time.localtime()
    print(time1)
    if time1.tm_mon ==alex.birth.mouth and time1.tm_mday == alex.birth.day:
        print("生日快乐")
    else:print("今天不是他生日")
    View Code
    人狗大战升级版
    class Equip:
        def __init__(self,name,price,aggr):
            self.name=name
            self.price=price
            self.aggr=aggr
        def raid(self,dog):
            dog.hp-=self.aggr
            print("用%s袭击了%s,%s的生命值还剩%s"%(self.name,dog.name,dog.name,dog.hp))
    class Person:
        def __init__(self,name,sex,aggr,hp):
            self.name = name
            self.sex = sex
            self.aggr = aggr
            self.hp = hp
        def attack(self,dog):
            dog.hp -= self.aggr
            print('%s打了%s,%s的生命值还剩%s'%(self.name,dog.name,dog.name,dog.hp))
    class Dog:
        def __init__(self,name,kind,aggr,hp):
            self.name = name   # 对象属性
            self.kind = kind
            self.aggr = aggr
            self.hp = hp
        def bite(self,person):
            person.hp -= self.aggr
            if person.hp>0:
                print('%s咬了%s,%s的生命值还剩%s' % (self.name, person.name, person.name, person.hp))
            else:print('%s咬了%s,%s已死亡' % (self.name, person.name, person.name))
    weason=Equip("百步飞剑",2000,5000)
    hei = Dog('小黑','teddy',260,10000)
    alex = Person('alex','female',150,8000)
    import time
    while True:
        if alex.hp>0:
            alex.attack(hei)
            time.sleep(0.3)
            hei.bite(alex)
            time.sleep(0.3)
        elif alex.hp <= 0:
            choice=input("输入y可以复活,其他则退出:")
            if choice == "y":
                money=int(input("复活一次需要1000,请输入你充值的金额[q/退出]:"))
                if money != "q":
                    alex.hp = 260
                    alex.money = money - 1000
                    print("恭喜你复活了,你的余额为%d"%(alex.money))
                    ret=input("输入y可以购买装备,其他则退出:")
                    if ret == "y":
                        alex.money-=weason.price
                        alex.wea=weason
                        print("恭喜你购买了%s,你的余额为%d,当前武器为%s"%(weason.name,alex.money,alex.wea.name))
                        alex.wea.raid(hei)
                        time.sleep(0.3)
                        hei.bite((alex))
    View Code
     
     
    
    


     
  • 相关阅读:
    水利行业传感器
    含水量传感器
    水位传感器
    物联网实践
    SQLCMD
    zigbee
    物联网支撑平台
    近距通信技术比较
    物联网发展
    NFC标签
  • 原文地址:https://www.cnblogs.com/zgf-666/p/8541191.html
Copyright © 2011-2022 走看看