2020-08-23

方法没有重载,多个重名的方法,只有最后一个方法有效。
class Person:
def work(self):
print(“努力工作!”)
def play_games(s):
print("{0}在玩游戏".format(s))
def work2(self):
print(“努力上班,努力工作”)
Person.play=play_games
Person.work=work2
p=Person()
p.work()
p.play()

努力上班,努力工作
<main.Person object at 0x0026C450>在玩游戏

私有属性的私有方法d

两个下划线开头的属性是私有的,类内部可以访问私有属性,类外部不能直接访问,
可通过_类名__私有属性名
class Student:
def init(self,name,age):
self.name=name
self.__age=age 两条下划线表示私有属性(私有方法同理)
s=Student(“组织”,25)
print(s.name)
print(s._Student__age) 通过_类名__私有属性名 访问

组织
25

私有方法同理
class Employee: get和set方法
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<salary<50000:
self.__salary=salary
else:
print(“录入错误!”)
emp1=Employee(“高”,30000)
print(emp1.get_salary())
emp1.set_salary(1190)
print(emp1.get_salary())

30000
1190

装饰器
class Employee:
def init(self,name,salary):
self.__name=name
self.__salary=salary
@property
def salary(self):
return self.__salary
@salary.setter
def salary(self,salary):
if 1000<salary<50000:
self.__salary=salary
else:
print(“录入错误!”)
emp1=Employee(“高”,30000)
print(emp1.salary)
emp1.salary=-90
print(emp1.salary)

30000
录入错误!
30000

封装:隐蔽对象的属性和实现细节,对外提供必要的方法,细节封装起来。
继承:让子类具有父类的特性,提高代码的重用性,父类就是基类,子类就是派生类
多态:同一个方法调用,由于对象不同,产生不同的行为

继承语法:多重继承,一个子类可以继承多个父类。
class 子类类名(父类1,父类2)

class Person:
def init(self,name,age):
self.name=name
self.age=age
def say_salary(self):
print(“加油就是胜利”)
class Student(Person) :
def init(self,name,age,score):
Person.init(self,name,age)
self.score=score
x=Student(“gao”,12,90)
x.say_salary()
print(x.name)
加油就是胜利
gao

class Person:
def init(self,name,age):
self.name=name
self.age=age
def say_salary(self):
print(“加油就是胜利”)
class Student(Person) :
def init(self,name,age,score):
Person.init(self,name,age)
self.score=score
x=Student(“gao”,15,90)
x.say_salary()
print(x.name)

加油就是胜利
gao

方法的重写:子类继承了父类的方法,并重新定义了父类的方法。
class Person:
def init(self,name,age):
self.name=name
self.age=age
def say_age(self):
print(“我的年龄是{0}”.format(self.age))
def introduce_name(self):
print(“我的姓名是{0}”.format(self.name))
class Student(Person) :
def init(self,name,age,score):
Person.init(self,name,age)
self.score=score

def introduce_name(self):
    print("大家好!我的姓名是{0}".format(self.name))

x=Student(“gao”,15,90)
x.say_age()
x.introduce_name()

我的年龄是15
大家好!我的姓名是gao

object所有属性,子类都有,object是根类。

__str__方法重写
class Person:
def init(self,name):
self.name=name
def str(self):
return “我的名字是{0}”.format(self.name)

s=Person(“小”)
print(s)

我的名字是小

MRO方法解析顺序,从左往右先用B父类,class C(B,A)

class A:
def say(self):
print(“aa”)
class B:
def say(self):
print(“bb”)
class C(B,A):
def cc(self):
print(“cc”)
c=C()
print(C.mro())
c.say()

[<class ‘main.C’>, <class ‘main.B’>, <class ‘main.A’>, <class ‘object’>]
bb

在子类中,如果想要获得父类方法,通过super()来做,代表父类的定义,而不是对象
class A:
def say(self):
print(“A:”,self)
class B(A):
def say(self):
super().say()
# A.say(self)
print(“B:”,self)

B().say()

A: <main.B object at 0x01E66670>
B: <main.B object at 0x01E66670>

多态:方法的多态,属性没有多态,两个继承条件,继承和重写。

class Man:
def eat(self):
print(“肚子饿了可以吃饭了”)
class ChineseMan(Man):
def eat(self):
print(“用筷子吃饭了”)
class EnglishMan(Man):
def eat(self):
print(“用刀叉吃饭了”)
class IndianMan(Man):
def eat(self):
print(“用右手吃饭了”)
def maneat(m):
if isinstance(m,Man):

用右手吃饭了

特殊方法和运算符重载
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.nameother
else:
return(“不能相乘”)
p1=Person(“高”)
p2=Person(“手”)
p=p1+p2
print§
print(p
5)

高-手
高-手高-手高-手高-手高-手

浅拷贝:只拷贝对象,对象包含的子对象不拷贝。
深拷贝:递归拷贝对象,包含子拷贝。
import copy
class MobilePhone:
def init(self,cpu,screen):
self.cpu=cpu
self.screen=screen
class Cpu:
def calculate(self):
print(“算你666”)
print(“计算对象:”,self)
class Screen:
def show(self):
print(“好美好美!”)
print(“图片对象:”,self)
m1=Cpu()
m2=m1
print(m1)
print(m2)
s1=Screen()
qq=MobilePhone(m1,s1)
ss=copy.copy(qq)
print(qq,qq.cpu,qq.screen)
print(ss,ss.cpu,ss.screen)
print(“nonononononono”)
cc=copy.deepcopy(qq)
print(qq,qq.cpu,qq.screen)
print(cc,cc.cpu,cc.screen)

<main.Cpu object at 0x01EC6750>
<main.Cpu object at 0x01EC6750>
<main.MobilePhone object at 0x01EC6E50> <main.Cpu object at 0x01EC6750> <main.Screen object at 0x01EC6770>
<main.MobilePhone object at 0x01ED3370> <main.Cpu object at 0x01EC6750> <main.Screen object at 0x01EC6770>
nonononononono
<main.MobilePhone object at 0x01EC6E50> <main.Cpu object at 0x01EC6750> <main.Screen object at 0x01EC6770>
<main.MobilePhone object at 0x01EDD690> <main.Cpu object at 0x01EDDC70> <main.Screen object at 0x01EDDCB0>

is a关系:狗是动物,继承。
has a关系:手机有cpu,组合。

class A1: 继承关系:
def say_a1(self):
print(“n1nn1n1”)
class B1(A1):
pass
b1=B1()
b1.say_a1()

class A1: 组合关系
def say_a1(self):
print(“n1nn1n1”)
class B1:
def init(self,a):
self.a=a
a1=A1()
b1=B1(a1)
b1.a.say_a1()

class CarFactory: 工厂模式
def create_car(self,brand):
if brand==“奔驰”:
return Benz()
elif brand == “宝马”:
return BWM()
elif brand == “宝马”:
return “BMW”
else:
return(“不知名品牌”)
class Benz:
pass
class BWM:
pass
Factory=CarFactory()
x=Factory.create_car(“宝马”)
print(x)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值