您的位置:首页 > 编程语言 > Python开发

python基础-命名空间、类_对象调用范围、对象组合

2017-11-20 16:44 615 查看
面向对象的命名空间

可变不可变类型

类对象调用方法的示例

_dict_

不能使用global

类获取对象属性

对象组合

面向对象的命名空间

1、类名操作可变、不可变数据类型,都是类中对应的变量发生变化

2、对象名操作静态变量

2_1引用变量:在自己的命名空间中查找,找不到就去类的命名空间找

3、修改变量:

3_1如果是对可变数据类型中的元素进行修改,那么全局生效

3_2如果是对变量进行重新赋值,那么只是在对象自己的命名空间里增加了一个新的属性

结论:应该尽量用类名去使用静态变量

class Foo:
country = 'China'
country_lst = ['China']
def __init__(self,name):
self.name = name

egg = Foo('egon')
safly = Foo('safly')
print(safly.country,safly.country_lst)

safly.age = 90
safly.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.面向对象的命名空间.py
China ['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 = country

alex = Foo('alexander',"印度")
print(alex.country)

#删除对象属性,会查询类属性
del alex.country
print(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.py
method1 1
None
--------
out method
--------
method2 111
--------
method1 333
--------
method1 333

Process finished with exit code 0


_dict_

init方法可以不写

#init可以不写
class Person:
def eat(self):
pass

alex = 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.name

p1 = Person("safly1")
#获取对象属性
print(Person.getName(p1))


输出如下:

E:\python\python_sdk\python.exe E:/python/py_pro/python.py
safly1

Process 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 = day
boss_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.组合.py
1968
12
31

Process finished with exit code 0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐