1.多态
#用法1:
class Intend_Todo(object):
def __init__(self,name):
self.name=name
def skip_class(self):
print('跑路')
def further_study(self):
print('深入学习')
class Inquire():
def __init__(self,name):
self.name=name
def help(self):
print('寻求帮助')
def ways(self):
print('寻找研究方法')
class Gain():
pass
something=Intend_Todo('security')
somewhat=Inquire('opportunities')
somehow=Gain()
def attain(obj):
print('Do you know %s'%obj.name)
attain(somewhat)
#运行结果#
Do you know opportunities
# #用法2:无继承关系
class Intend_Todo(object):
def skip_class(self):
print('跑路')
def skip_class(self):
print('深入学习')
class Inquire(object):
def skip_class(self):
print('寻求帮助')
def skip_class(self):
print('寻找研究方法')
def gain(obj):
obj.skip_class()
something=Intend_Todo()
gain(something)
something=Intend_Todo()
gain(something)
somewhat=Inquire()
gain(somewhat)
somewhat=Inquire()
gain(somewhat)
#运行结果#
深入学习
深入学习
寻找研究方法
寻找研究方法
#第二种:有继承
class Intend_Todo(object):
def help(self):
print('跑路')
def help(self):
print('深入学习')
class Inquire(Intend_Todo):
def help(self):
super().help()
print('寻求帮助')
def help(self):
super().help()
print('寻找研究方法')
def gain(obj):
obj.help()
something=Inquire()
gain(something)
#运行结果#
深入学习
寻找研究方法
2.属性和方法
- 修改属性方法:对象.属性名=属性值
2.1类属性
- 类属性是直接定义在类中的属性
- 类属性可以通过类对象和该例的实例对象来访问
- 类属性只能通过类对象来修改,不能通过实例对象来修改
class Interests(object):
hobby='Python'
def change(self):
cultivated.hobby='draw' #类对象不能通过实例对象修改
print(Interests.hobby) #通过类对象来访问
cultivated=Interests() #创建实例对象
print(cultivated.hobby) #通过实例对象来访问
Interests.hobby='sing' #通过类对象来修改 修改属性:对象.属性名=属性值
print(Interests.hobby)
cultivated=Interests()
print(cultivated.hobby)
#运行结果#
Python
Python
sing
sing
2.2实例属性
- 实例属性:实例对象可以访问其,类对象不可以
class Interests(object):
def change(self):
cultivated.hobby='draw'
cultivated=Interests()
print(Interests.hobby)#类对象不可以访问实例属性
#运行结果#
AttributeError: type object 'Interests' has no attribute 'hobby'
2.3实例方法
class Interests(object):
def change(self):
cultivated.hobby='draw'
print(cultivated.hobby)
cultivated=Interests()
cultivated.change() #通过实例对象调用实例方法
Interests.change(cultivated) #通过类对象调用实例方法 传递一个实例对象作为参数
#运行结果#
draw
draw
2.4类方法
- 需要使用@classmethod装饰
- 可以任由实例对象和类对象调用,不需要传递任何参数
class Interests(object):
@classmethod #类方法
def change(self):
cultivated.hobby='draw'
print(cultivated.hobby)
cultivated=Interests()
cultivated.change()
Interests.change()
#运行结果#
draw
draw
class Interests(object):
@classmethod
def change(self):
cultivated.hobby='draw'
print(cultivated.hobby)
@staticmethod #静态方法
def change1(): #静态方法中,定义方法括号内不用参数
cultivated.hobby='ring'
print(cultivated.hobby)
cultivated=Interests()
cultivated.change1() #实例对象可以直接调用方法,无需参数
Interests.change1()#类对象可以直接调用静态方法,无需参数
3.__new__魔法方法
创建对象时,Python解释器会先调用__new__方法创建对象并返回,然后再调用__init__对创建好的对象进行初始化:
class Test: pass test = Test() #解释器的工作过程 #1.test = object.__new__(Test) #2.object.__init__(test) #因为Test没定义__init__方法,所以采用基类的方法,object是所有类的基类
如果重写了__new__方法,就必须显式的调用基类的__new__方法创建对象返回,__new__是解释器实现的类方法:
class Demo(object):
def __new__(cls, *args, **kwargs):
self=super().__new__(cls) #也可以直接写为object.__new__(cls, *args, **kwargs),object类是所有类的基类
print('__new__')
return self
def __init__(self):
print('__init__')
test=Demo()
4.单例模式
class Demo(object):
a = 1
def demo(self, cls):
cls.a = 2
print(cls.a)
d1 = Demo()
d1.demo(Demo)
#运行结果#
2
class People(object):
colour=None
def __new__(cls, *args, **kwargs):
if cls.colour == None:
cls.colour=super().__new__(cls)
return cls.colour
else:
return cls.colour
person=People()
person1=People()
print(person)
print(person1)
#运行结果#
2034983779248
2034983779248
5.模块的使用
- import …
- from … import …