极客学院面向对象程序设计
属性代表对象的数据,方法代表对象的操作
#方法及属性的实现
class woman:
pass
wangdama = woman()
#查看一个实例具有哪些属性
print wangdama.__dict__
#为实例添加属性
wangdama.toufa = "huangse"
#查看实例所属类的属性
# print wangdama.__class__.__dict__
#给实例所属类添加属性
wangdama.__class__.xiezi = "heise"
#方法的创建
class god:
def a(self): #self代表所有实例共享它
print "所有人必须每天唱一首歌"
zongguan= god()
zongguan.a() #方法的调用
#隐藏属性与隐藏方法
class school:
def __jiaoxuefangfa(self): #前面加双下划线
print " &^^*&*"
#常见的一些专有方法
#1. __init__ 构造函数
class people:
def hi(self):
print 8899
def __init__(self):
a = "A:how are you?"
b = "---B:Fine, thank you"
print a+b
people()
#输出
A:how are you?---B:Fine, thank you
#2. __del__ 析构函数
class friend:
def hi(self):
print(8899)
def __init__(self):
print("我是init最先调用")
def __del__(self):
a = "我是析构函数"
b = "---对象生命周期结束啦,现在我得删除对象善后啦!"
print(a + b)
xiaohang = friend()
xiaohang.hi()
#friend()
#friend().hi()
#输出
我是init最先调用
8899
我是析构函数---对象生命周期结束啦,现在我得删除对象善后啦!
#Python中多继承方法冲突解决
class f(a,b):
pass
对于继承的父类A,B,当有方法冲突时,优先继承A的方法