class Person:
def say_hi(self,name):
print("{0}hello".format((name)))
p1=Person()
p1.say_hi("高崎")
输出为
高崎11111hello
class Student:
def work(self,name):
print("{0}努力上班".format(name))
def play_game(s,name):
print("{0}在玩游戏".format(name))
def work2(s):
print("好好工作,努力赚钱娶媳妇")
Student.play=play_game
p2=Student()
p2.work("欧文")
p2.play("赵薇")#Person.play(p)
Student.work=work2#修改了方法,不再调用类里面的work
p2.work()
def f4(score):
print("{0}的成绩".format(score))
f4(86)
输出为
欧文努力上班
赵薇在玩游戏
好好工作,努力赚钱娶媳妇
86的成绩
class Employee:
__company="百战程序员"
def __init__(self,name,age):
self.name=name
self.__age=age#变为私有属性
def __work(self):#私有的方法
print("好好工作,我是最棒的")
print("年龄{0}".format(self.__age))#类内部可以直接调用自己的私有属性
print(Employee.__company)#类内部可以直接调用私有的类变量
e=Employee("高崎",18)
print(e.name)
#print(e.age)#私有的不能直接访问
print(e._Employee__age)#私有属性的调用
e._Employee__work()#私有方法的调用
print(Employee._Employee__company)#私有类变量的调用
输出为
高崎
18
好好工作,我是最棒的
年龄18
百战程序员
百战程序员
class Employee1:
@property
def salary(self):
#print("salary run")
return 1000
s4=Employee1()
#s4.salary()#普通的调用方法
print(s4.salary)#变成属性的调用
#s4.salary=2000本条语句是错的,引文不能修改属性,只能使用
#@property装饰器最简化的使用
#普通方法的比较与@property
class Employee2:
def __init__(self,name,salary):
self.__name=name
self.__salary=salary
def get_salary(self):
return self.__salary
def set_salary(self,salary):
if 1000
输出为
1000
30000
20000
30000
录入错误!薪水在1000-50000这个范围
30000
class Person:
def __init__(self,name,age):
self.name=name
self.__age=age#若是私有的属性不能直接用
def sayage(self):
print("你好")
class Student(Person):
def __init__(self,name,age,score):
Person.__init__(self,name,age)#必须显示的去调用父类的初始方法,不然解释器不会去调用
self.score=score
print(Student.mro())#mro可以输出这个类的继承层次结构
s=Student("高崎",18,60)
s.sayage()
# print(s.age)
print(s.name)
print(dir(s))
print(s._Person__age)
输出为
[
main.Student’>, main.Person’>, ]
你好
高崎
[’_Person__age’, ‘class’, ‘delattr’, ‘dict’, ‘dir’, ‘doc’, ‘eq’, ‘format’, ‘ge’, ‘getattribute’, ‘gt’, ‘hash’, ‘init’, ‘init_subclass’, ‘le’, ‘lt’, ‘module’, ‘ne’, ‘new’, ‘reduce’, ‘reduce_ex’, ‘repr’, ‘setattr’, ‘sizeof’, ‘str’, ‘subclasshook’, ‘weakref’, ‘name’, ‘sayage’, ‘score’]
18
class Person:
def __init__(self,name,age):
self.name=name
self.__age=age#若是私有的属性不能直接用
def sayage(self):
print("我的年龄",self.__age)
def sayintroduce(self):
print("我的自我介绍{}".format(self.name))
class Student(Person):
def __init__(self,name,age,score):
Person.__init__(self,name,age)#必须显示的去调用父类的初始方法,不然解释器不会去调用
self.score=score
def sayintroduce(self):
print("报告老师,我的名字是{}".format(self.name))
s=Student("高崎",12,89)
s.sayage()
s.sayintroduce()
#2.1测试重写object的_str_()方法
class Person:
def __init__(self,name):
self.name=name
def __str__(self):
return "名字是:{0}".format(self.name)
p=Person("高崎")
print(p)
输出为
我的年龄 12
报告老师,我的名字是高崎
名字是:高崎
class A:
def aa(self):
print("aa")
def say(self):
print("say AAA")
class B:
def bb(self):
print("bb")
def say(self):
print("say BBB")
class C(B,A):
def cc(self):
print("cc")
c=C()
c.cc()
c.aa()
c.bb()
c.say()
输出为
cc
aa
bb
say BBB
class A:
def say(self):
print("A",self)
class B(A):
def say(self):
#A.say(self)#调用父类中的方法和super()方法作用一样
super().say()
print("B:",self)
B().say()
输出为
A <main.B object at 0x000001A18786C940>
B: <main.B object at 0x000001A18786C940>
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(Chinese())
maneat(Indian())
maneat(English())
输出为
中国人用筷子吃饭
印度人用手吃饭
美国人用叉子吃饭
a=20
b=30
c=a+b
d=a.__add__(b)
print("c=",c)
print("d=",d)
#6.1重写__add__方法
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 "不是同类对象不能相加"
def __mul__(self, other):
if isinstance(other, int):
return self.name*other
else:
return "不是同类对象不能相加"
p1=Person("高崎")
p2=Person("赵薇")
x=p1+p2
print(x)
print(p1*30)
输出为
c= 50
d= 50
高崎-赵薇
高崎高崎高崎高崎高崎高崎高崎高崎高崎高崎高崎高崎高崎高崎高崎高崎高崎高崎高崎高崎高崎高崎高崎高崎高崎高崎高崎高崎高崎高崎
class A:
def aa(self):
print("aa")
def say(self):
print("say AAA")
class B:
def bb(self):
print("bb")
def say(self):
print("say BBB")
class C(B,A):
def __init__(self,nn):
self.nn=nn
def cc(self):
print("cc")
c=C(3)
c.cc()
c.aa()
c.bb()
c.say()
print(dir(c))
print(c.__dict__)
print(c.__class__)
print(C.__bases__)
print(C.mro())
print(A.__subclasses__())
输出为
cc
aa
bb
say BBB
[‘class’, ‘delattr’, ‘dict’, ‘dir’, ‘doc’, ‘eq’, ‘format’, ‘ge’, ‘getattribute’, ‘gt’, ‘hash’, ‘init’, ‘init_subclass’, ‘le’, ‘lt’, ‘module’, ‘ne’, ‘new’, ‘reduce’, ‘reduce_ex’, ‘repr’, ‘setattr’, ‘sizeof’, ‘str’, ‘subclasshook’, ‘weakref’, ‘aa’, ‘bb’, ‘cc’, ‘nn’, ‘say’]
{‘nn’: 3}
main.C’>
(main.B’>, main.A’>)
[main.C’>, main.B’>, main.A’>, ]
[main.C’>]
#深拷贝
class MobilePhone:
pass
class CPU:
pass
class Scree:
pass
m1=MobilePhone()
m2=m1
print(m1)
print(m2)
#浅拷贝
import copy
class MobilePhone:
def __init__(self,cpu,screen):
self.cpu=cpu
self.screen=screen
class CPU:
def calculate(self):
print("算你个123")
print("cpu对象",self)
class Screen:
def show(self):
print("显示一个好看的画面")
print("screen",self)
#测试变量赋值
c1=CPU()
c2=c1
print(c1)
print(c2)
#测试浅拷贝
s1=Screen()
m3=MobilePhone(c1,s1)
m4=copy.copy(m3)
print(m3,m3.cpu,m3.screen)
print(m4,m4.cpu,m4.screen)
输出为
<main.MobilePhone object at 0x000001A18786CBE0>
<main.MobilePhone object at 0x000001A18786CBE0>
<main.CPU object at 0x000001A18786CCC0>
<main.CPU object at 0x000001A18786CCC0>
<main.MobilePhone object at 0x000001A18786CD30> <main.CPU object at 0x000001A18786CCC0> <main.Screen object at 0x000001A18786CCF8>
<main.MobilePhone object at 0x000001A18786CE10> <main.CPU object at 0x000001A18786CCC0> <main.Screen object at 0x000001A18786CCF8>
#9.1继承
class A1:
def saya1(self):
print("hello word")
class B1(A1):
pass
b1=B1()
b1.saya1()
#9.2组合
class A2:
def saya1(self):
print("hello word")
class B2:
def __init__(self,a):
self.a=a
a2=A2()
b2=B2(a2)
b2.a.saya1()
#9.3测试has-a关系,使用组合
class MobilePhone:
def __init__(self,cpu,screen):
self.cpu=cpu
self.screen=screen
class CPU:
def calculate(self):
print("算你个123")
print("cpu对象",self)
class Screen:
def show(self):
print("显示一个好看的画面")
print("screen",self)
l=MobilePhone(CPU(),Screen())
l.cpu.calculate()
l.screen.show()
输出为
hello word
hello word
算你个123
cpu对象 <main.CPU object at 0x000001A18786CFD0>
显示一个好看的画面
screen <main.Screen object at 0x000001A187874048>
class CarFactory:
def credate_car(self,brand):
if brand=="奔驰":
return Benz()
elif brand=="宝马":
return BMW()
elif brand=="比亚迪":
return BYD()
else:
return "未知品牌无法生产"
class Benz:
pass
class BMW:
pass
class BYD:
pass
factory=CarFactory()
f1=factory.credate_car("奔驰")
f2=factory.credate_car("比亚迪")
print(f1)
print(f2)
输出为
<main.Benz object at 0x00000255677DEEF0>
<main.BYD object at 0x00000255674DD908>
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("aa")
b=MySingleton("bb")
print(a)
print(b)
c=MySingleton("cc")
print(c)