zoukankan      html  css  js  c++  java
  • 面向对面 静态参数 与 组合

    #  ======================            面向对象的 讲解  和 组合  ======
    # 命名空间

    # class Person:
    # Country = '中国人' # 静态变量

    # print(Person.Country)
    # alex = Person() # 创建了一个空的命名空间
    # alex.name = 'alex' # 对象
    # alex.Country = '泰国人'
    # egon = Person()
    # egon.name = 'egon'

    # 类名.静态变量 对象.属性名
    # 类名可以调用对象的属性么? 不可以
    # 对象可以调用类中的属性么? 可以
    # print(Person.Country)
    # print(alex.Country)
    # print(egon.Country)

    # 由于对象和类之间存在一个关联关系
    # 所以对象能够找到类
    # 但是 类不能找到对象

    #使用类名.属性 只会寻找类中的静态变量名字
    #使用对象.属性 会现在对象自己的命名空间中找名字
    # 如果找不到 再到类的内存空间中去找

    # class Person:
    # Country = '中国人' # 静态变量
    #
    # alex = Person()
    # egon = Person()
    # print(alex.Country)
    # alex.Country = '印度人'
    # print(alex.Country)
    # Person.Country

    # 只要你使用静态变量 就用类名去调用
    # class Person:
    # money = 0
    #
    # mother = Person()
    # father = Person()
    # Person.money += 1000
    # Person.money += 1000
    # print(mother.money)
    # print(father.money)
    # print(Person.money)

    # class Person:
    # money = [0]
    #
    # mother = Person()
    # father = Person()
    # mother.money[0] += 1000
    # father.money[0] += 1000
    # print(mother.money)
    # print(father.money)
    # print(Person.money)

    # class Person:
    # money = [0]
    #
    # mother = Person()
    # father = Person()
    # mother.money = [1000]
    # father.money = [2000]
    # print(mother.money)
    # print(father.money)
    # print(Person.money)

    # a = 1
    # a = 2
    # print(a)
    # a = [1]
    # a.append(2)
    # print(id(a))
    # a[0] = 10
    # print(id(a))
    # print(a)
    # a = [1]
    # a = [2]

    # 写一个类,能统计这个类被多少个对象实例化了.
    # 所有的对象共享这个结果
    # init 静态变量

    # class Foo:
    # num = 0
    # def __init__(self):
    # Foo.num += 1
    #
    # f1 = Foo()
    # print(Foo.num)
    # f2 = Foo()
    # print(Foo.num)
    # print(f1.num)


    # ======================== ============ 组合 =================================
    # 组合 两个类的事儿
    # 什么叫组合 : 一个类对象的属性 是 另外一个类的对象
    # 两个类的事儿 :类与类之间有一种"什么有什么的关系"

    # 圆的类
    # 圆环 和 圆
    # 圆环 也是一个类
    # 属性 大圆半径 和 小圆半径
    # 圆环 求面积 求周长

    # 圆环
    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
    # # c1 = Circle(5)
    # # c1.perimeter()
    # class Ring:
    # def __init__(self,outer,inner):
    # self.outer = Circle(outer) # 组合
    # self.inner = Circle(inner)
    # def area(self):
    # return self.outer.area() - self.inner.area()
    # def perimeter(self):
    # return self.outer.perimeter() + self.inner.perimeter()
    # class Ring:
    # def __init__(self,outer,inner):
    # self.outer = outer
    # self.inner = inner
    # def area(self):
    # return pi*(self.outer**2) - pi*(self.inner**2)
    # def perimeter(self):
    # return 2*pi*self.outer + 2*pi*self.inner
    # r = Ring(10,5)
    # r.area()
    # r.perimeter()

    #组合例子 2
    # class Birthday:
    # def __init__(self,year,month,day):
    # self.year = year
    # self.month = month
    # self.day = day
    # class Teacher:
    # def __init__(self,name,sex,course,birth):
    # self.name = name
    # self.sex = sex
    # self.course = course
    # self.birth = birth # birth是一个对象
    # birth = Birthday(1960,3,7) # birth 是Birthday类的一个对象
    # alex = Teacher('alex','male','python',birth)
    # class Bir:
    # def __init__(self,year,month,day):
    # self.year = year
    # self.month = month
    # self.day = day
    # class Tea:
    # def __init__(self,name,sex,course,birth):
    # self.name = name
    # self.sex = sex
    # self.course = course
    # self.birth = birth
    # birth = Bir(1999,3,6)
    # taibai = Tea("taibai","male","python",birth)
    # import time
    # if birth.month == time.localtime().tm_mon and birth.day == time.localtime().tm_mday:
    # print("生日快乐")
  • 相关阅读:
    使用外部 toolchain 编译 openwrt
    openwrt network 初始化
    attribute constructor&destructor
    ditaa
    quilt
    转载
    无线网络的一些基础概念
    FIR300M刷openwrt
    翻译:A Tutorial on the Device Tree (Zynq) -- Part V
    翻译:A Tutorial on the Device Tree (Zynq) -- Part IV
  • 原文地址:https://www.cnblogs.com/xuerh/p/8515943.html
Copyright © 2011-2022 走看看