print('hello world')
#类
##方法的动态性
class Person:
def study(self):
print('好好学习')
def play_games(s):
print('{0}在玩游戏'.format(s))
def study2(s):
print('好好学习天天向上')
Person.play=play_games
p=Person()
p.study()
p.play()
Person.study=study2
p.study()
##私有属性
class employee:
def __init__(self,name,age):
employee.name=name
employee.__age=age
def __work(self):
print('好好工作')
def study(self):
print('好好学习')
e=employee('yang',20)
print(e.name)
#print(e.age)
print(e._employee__age)
e._employee__work()
e.study()
#@property装饰器的用法
class student():
def __init__(self,name,score):
self.__name=name
self.__score=score
@property
def score(self):
return self.__score
@score.setter
def score(self,score):
if 0 < score < 100:
self.__score = score
else:
print('输入错误,分数在0--100之间')
'''
def get_score(self):
return self.__score
def set_score(self,score):
if 0<score<100:
self.__score=score
else:
print('输入错误,分数在0--100之间')
a=student('yang',100)
print(a.get_score())
a.set_score(3)
print(a.get_score())
a.set_score(3333)
print(a.get_score())
'''
a=student('yang',100)
print(a.score)
a.score=90
print(a.score)
print(a._student__name)
a._student__name='zhang'
print(a._student__name)
##继承的基本用法
class person:
def __init__(self,name,age):
self.name=name
self.age=age
def say_age(self):
print('我也不知道')
class student(person):
def __init__(self,name,age,score):
person.__init__(self,name,age)
self.score=score
s=student('yang',19,70)
s.say_age()
print('名字是{0},年龄是{1},分数是{2}'.format(s.name,s.age,s.score))
###重写父类
class person:
def __init__(self,name,age):
self.name=name
self.age=age
def say_age(self):
print('我的年龄是:',self.age)
def say_name(self):
print('我的名字是:',self.name)
class student(person):
def __init__(self,name,age,score):
person.__init__(self,name,age)
self.score=score
def say_name(self):
print('报告老师,我的名字是:',self.name)
s=student('yang',19,70)
s.say_age()
print('名字是{0},年龄是{1},分数是{2}'.format(s.name,s.age,s.score))
s.say_age()
s.say_name()
###重写object的__str__()
class Person:
def __init__(self,name):
self.name=name
def __str__(self):
return '名字是:'
p=Person('yang')
print(p)
###多重继承
class A:
def aa(self):
print('aa')
class B:
def bb(self):
print('bb')
class C(A,B):
def cc(self):
print('cc')
c=C()
c.aa()
c.bb()
c.cc()
###super()
class A:
def say(self):
print('A:',self)
class B(A):
def say(self):
####A.say(self)
super().say()
print('B:',self)
B().say()
##多态
class man:
def eat(self):
print('吃饭了')
class Chinese(man):
def eat(self):
print('中国人用筷子吃饭')
class English(man):
def eat(self):
print('英国人用叉子吃饭')
class Indian(man):
def eat(self):
print('印度人用手吃饭')
def manEat(m):
if isinstance(m,man):
m.eat()
else:
print('没法吃饭')
manEat(English())
manEat(3)
##运算符重载
class person:
def __init__(self,name):
self.name=name
def __add__(self,other):
if isinstance(other,person):
return '{0}--{1}'.format(self.name,other.name)
else:
return '无法想加'
a=person('yang')
b=person('zhang')
print(a+b)
##对象的浅拷贝和深拷贝
import copy
class phone:
def __init__(self,cpu,screen):
self.cpu=cpu
self.screen=screen
class cpu:
def calculate(self):
print('算你个大头鬼')
print('cpu:',self)
class screen:
def show(self):
print('亮瞎你的狗眼')
print('screen:',self)
c=cpu()
s=screen()
p1=phone(c,s)
p2=copy.copy(p1)
p3=copy.deepcopy(p1)
print(p1,p1.cpu,p1.screen)
print(p2,p2.cpu,p2.screen)
print(p3,p3.cpu,p3.screen)
##组合
p=phone(cpu(),screen())
p.cpu.calculate()
p.screen.show()
###工厂模式
class carFactory:
def car(self,brand):
if brand=='奔驰':
return Benz()
elif brand=='宝马':
return BWM()
elif brand=='比亚迪':
return BYD()
else:
return "没有该型号"
class Benz:
pass
class BWM:
pass
class BYD:
pass
a=carFactory().car
print(a('宝马'))
print('******************************************8')
###单例模式
class MySingleton:
__obj=None
__init_flag=True
def __new__(cls,*args,**kwargs):
if cls.__obj==None:
cls.__obj=object.__new__(cls)
return cls.__obj
def __init__(self,name):
if MySingleton.__init_flag:
print('init..')
self.name=name
MySingleton.__init_flag=False
a=MySingleton('a')
b=MySingleton('b')
print(a)
print(b)
类,面向对象(继承,子类父类,多态,组合,设计模式)
最新推荐文章于 2024-11-11 08:32:59 发布