面向对象进阶2 组合

news/2024/7/5 1:51:54

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

一:命名空间

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)

打印结果

163244_kH8m_3657436.png

注意:类名不可以调用对象的属性(不能清楚具体是哪个对象属性,两个country)

          对象名可以调用类中的属性(alex.country)

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

 

二:对象.属性命名空间寻找(先在自己命名空间找,后再类命名空间找)

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

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

alex = Person()
egon = Person()
print(alex.Country)
alex.Country = '印度人'
print(alex.Country)
Person.Country# 只要你使用静态变量  就用类名去调用

注意:当你使用静态变量时候,就用类名去调用

例子1

class Person:
    money = 0

mother = Person()
father = Person()
Person.money += 1000
Person.money += 1000
print(Person.money)
print(mother.money)
print(father.money)

打印结果:2000,2000,2000

例子2

class Person:
    money = [1]mother = Person()
father = Person()
mother.money[0] += 1000   [0]表示索引
father.money[0] += 1000   [0]表示索引
print(mother.money)
print(father.money)
print(Person.money)

打印结果    [2001],[2001],[2001]

注意:[0]表示索引,+= 1000 就是索引加。

例子3

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

打印结果:【1000】,【2000】,【0】 例子3中索引改变才让调用类属性改变,而本题没有变化。

例子4

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]

打印结果:2,40615688,40615688,【10,2】

例子5:

写一个类,能统计这个类被多少个对象实例化了.
所有的对象共享这个结果
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)
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()
r=Ring(10,3)
c=Circle(11)
print(Ring.area(r))
print(r.area())

例题:老师生日组合

参考答案

老师  name sex course(课程) birth
生日  年月日
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)
# alex.birth = birth
# time
'1960-3-7'
# 老师的年龄  2018 - 1960
print(birth)
import time
if birth.month == time.localtime().tm_mon  and \birth.day == time.localtime().tm_mday:
    print('生日快乐')print(time.localtime().tm_year - birth.year)

自己写:

class Birthday:
    def __init__(self,year,month,day):
        self.year=year
        self.month=month
        self.day=day
class Teacher:
    def __init__(self,name,course,birth):
        self.name=name
        self.course=course
        self.birth=birth
birth=Birthday(1960,4,4)
zhen=Teacher('zhen','python',birth)
# print(Birthday.year())
print(zhen.birth.year)
print(birth.year)

转载于:https://my.oschina.net/u/3657436/blog/1630538


http://lihuaxi.xjx100.cn/news/237400.html

相关文章

区块链和可持续性

链客,专为开发者而生,有问必答! 此文章来自链客区块链技术问答社区,未经允许拒绝转载。 区块链和可持续性 在区块链算法和区块链平台方面,可持续性可以有多种解释。一方面,任何听说过比特币网络能源需求的…

python添加数组元素_Python列表附录–如何向数组添加元素,并附带示例说明

python添加数组元素欢迎 (Welcome) Hi! If you want to learn how to use the append() method, then this article is for you. This is a powerful list method that you will definitely use in your Python projects.嗨! 如果您想学习如何使用append()方法&…

Eclipse基金会发布Eclipse Photon IDE

Eclipse基金会发布了最新版本的Eclipse IDE。Eclipse Photon带来对Java 10和Java EE 8的支持,增强了PHP开发工具和Dark主题等功能。\\Eclipse Java开发工具(Eclipse Java Development Tools,JDT)对Java 10提供了完整的支持&#x…

C# 接口

1.接口的特点 接口的定义是指定一组函数成员而不实现成员的引用类型,其它类型和接口可以继承接口。定义还是很好理解的,但是没有反映特点,接口主要有以下特点: (1)通过接口可以实现多重继承,C#接口的成员不能有public、…

从 PoS 进化 SPoS:无能耗共识机制

链客,专为开发者而生,有问必答! 此文章来自链客区块链技术问答社区,未经允许拒绝转载。 从 PoS 进化 SPoS:无能耗共识机制 我们为 V SYSTEMS 区块链平台设计了了一个注重高扩展性的权益证明(PoS&#xff…

23岁一无所有怎么办_我搬到国外去创业,然后一无所有。

23岁一无所有怎么办以我的名字还不到一美元,它仍然感觉不像是最低点。 (With not even a dollar to my name, it still didn’t feel like rock bottom.) When you tell someone you’re working for a startup, they’ll either think you’re gonna be really ric…

中国电信打造“三朵云”战略 助力互联网+医疗发展

随着云计算、大数据的快速发展,全行业上云成为一个趋势,在健康医疗这个领域,应大势之趋,纷纷构建医疗云。近日,中国电信医疗云专区北京节点发布会在京顺利召开,会后北京电信副总经理项煌妹接受了中国IDC圈记…

jquery学习心得

$("p").click(function(){ $(this).hide(); }); 这个hide相当于$(this).css("display", "none"); 标签区域也不显示 转载于:https://www.cnblogs.com/lelezhangzhao/p/9429667.html