python基础-命名空间、类_对象调用范围、对象组合
来源:互联网 发布:木村拓哉 知乎 编辑:程序博客网 时间:2024/06/06 23:19
- 面向对象的命名空间
- 可变不可变类型
- 类对象调用方法的示例
- _dict_
- 不能使用global
- 类获取对象属性
- 对象组合
面向对象的命名空间
1、类名操作可变、不可变数据类型,都是类中对应的变量发生变化
2、对象名操作静态变量
2_1引用变量:在自己的命名空间中查找,找不到就去类的命名空间找
3、修改变量:
3_1如果是对可变数据类型中的元素进行修改,那么全局生效
3_2如果是对变量进行重新赋值,那么只是在对象自己的命名空间里增加了一个新的属性
结论:应该尽量用类名去使用静态变量
class Foo: country = 'China' country_lst = ['China'] def __init__(self,name): self.name = nameegg = Foo('egon')safly = Foo('safly')print(safly.country,safly.country_lst)safly.age = 90safly.country_lst = []safly.country_lst.append('印度')print(safly.age,safly.country,safly.country_lst)print(Foo.country,Foo.country_lst)print(egg.country,egg.country_lst)
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/3.面向对象的命名空间.pyChina ['China']90 China ['印度']China ['China']China ['China']Process finished with exit code 0
可变、不可变类型
class Foo: country = 'China' country_lst = ['China'] def __init__(self,name,country): self.name = name self.country = countryalex = Foo('alexander',"印度")print(alex.country)#删除对象属性,会查询类属性del alex.countryprint(alex.country)print(alex.country_lst)alex.country_lst.append('印度')print(alex.country_lst)egg = Foo('alexander',"印度")print(egg.country_lst)print(Foo.country_lst)
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/3.面向对象的命名空间.py印度China['China']['China', '印度']['China', '印度']['China', '印度']Process finished with exit code 0
类、对象调用方法的示例
def method(): print("out method")class Animal(): name = "animal" method = method def method1(self, a): print("method1",a) def method2(a): print("method2", a)a = Animal()print(a.method1(1))print("--------")#如下方式不行# a.method()#如下方式也不行# a.method2(2)#如下可以调用类外函数Animal.method()print("--------")Animal.method2(111)print("--------")Animal.method1(Animal,333)print("--------")Animal.method1(a,333)
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/python.pymethod1 1None--------out method--------method2 111--------method1 333--------method1 333Process finished with exit code 0
_dict_
init方法可以不写
#init可以不写class Person: def eat(self): passalex = Person()alex.eat()print(alex.__dict__)
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/python.py{}Process finished with exit code 0
不能使用global
我做了几个测试,是无法使用global的
类获取对象属性
#Person不能直接获取对象属性#但是可以通过方法间接获取class Person: name = "saf" def __init__(self,name): self.name = name #获取对象属性 def getName(self): return self.namep1 = Person("safly1")#获取对象属性print(Person.getName(p1))
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/python.pysafly1Process finished with exit code 0
对象组合
一个对象的属性是另外一个类的对象
class Teacher: def __init__(self,name,age,sex,year,month,day): self.name = name self.age = age self.sex = sex self.birth = Birthday(year,month,day)class Birthday: def __init__(self,year,month,day): self.year = year self.month = month self.day = dayboss_gold = Teacher('太亮',40,'不详',1968,12,31)print(boss_gold.birth.year)print(boss_gold.birth.month)print(boss_gold.birth.day)
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/4.组合.py19681231Process finished with exit code 0
阅读全文
0 0
- python基础-命名空间、类_对象调用范围、对象组合
- 基础复习------命名空间导入及访问类对象方法
- 对象,类,命名空间,继承......
- python的命名空间和对象
- 对象命名空间
- 命名空间的类的对象
- python基础-对象_类反射、模块反射
- python基础八_面向对象编程
- 面向对象进阶 01 对象、类、属性,方法、命名空间
- J2SE基础:3.对象的创建与使用类的命名空间和访问权限
- PHP基础----面向对象相关特性----10类和命名空间介绍
- Python 进阶_OOP 面向对象编程_组合与继承
- BW对象的命名空间
- 面向对象之命名空间
- php对象和命名空间
- python中类的命名空间和面向对象的诸多问题
- python中类的命名空间和面向对象的诸多问题
- Python 对象 --组合,绑定
- android studio查询代码行数的插件
- Readonly和Const的区别
- ios SDAutolayout的使用心得
- 构造函数,析构函数内调用虚函数问题
- Python exp() 函数
- python基础-命名空间、类_对象调用范围、对象组合
- 在eclipse中使用git
- 平均数编码:针对高基数定性特征(类别特征)的数据预处理/特征工程
- mysql命令补充
- three.js给场景加上阴影
- 汇编语言简单小程序——ASCII码转十进制数(bcd码)
- VAE(Variational Autoencoder)的原理
- HTTPS详细解析
- bzoj1821: [JSOI2010]Group 部落划分 Group(最小生成树)