目录
一.方法没有重载
Python 中没有方法的重载。定义多个同名方法,只有最后一个有效
class Person:
def say_hi(self):
print("hello")
def say_hi(self,name):
print("{0},hello".format(name))
p1 = Person()
#p1.say_hi() #不带参,报错:
TypeError: say_hi() missing 1
required positional argument: 'name'
p1.say_hi("高淇")
二.方法的动态性
Python 是动态语言,我们可以动态的为类添加新的方法,或者动态的修改类的已有的方法
class Person:
def work(self):
print("努力上班!")
def play_game(self):
print("{0}玩游戏".format(self))
def work2(s):
print("好好工作,努力上班!")
Person.play = play_game
Person.work = work2
p = Person()
p.play()
p.work()
Person 动态的新增了 play_game 方法,以及用 work2 替换了 work 方法。
三.私有属性和私有方法
1. 通常我们约定,两个下划线开头的属性是私有的(private)。其他为公共的(public)。
2. 类内部可以访问私有属性(方法)
3. 类外部不能直接访问私有属性(方法)
4. 类外部可以通过“_类名__私有属性(方法)名”访问私有属性(方法)
#测试私有属性、私有方法
class Employee:
__company = "百战程序员" #私有类属性. 通过 dir 可以查到 _Employee__company
def __init__(self,name,age):
self.name = name
self.__age = age #私有实例属性
def say_company(self):
print("我的公司是:",Employee.__company) #类内部可以直接访问私有属性
print(self.name,"的年龄是:",self.__age)
self.__work()
def __work(self): #私有实例方法,通过 dir 可以查到_Employee__work
print("工作!好好工作,好好赚钱,娶个媳妇!")
p1 = Employee("高淇",32)
print(p1.name)
print(dir(p1))
p1.say_company()
print(p1._Employee__age)
#通过这种方式可以直接访问到私有属 性 。通过 dir 可以查到属性:_Employee__age
#print(p1.__age) #直接访问私有属性,报错
#p1.__sleep() #直接访问私有方法,报错
四.@property 装饰器
@property 可以将一个方法的调用方式变成“属性调用”。
class Employee:
@property
def salary(self):
return 30000;
emp1 = Employee()
print(emp1.salary) #打印 30000
print(type(emp1.salary)) #打印<class 'int'>
#emp1.salary() #报错:TypeError: 'int' object is not callable
#emp1.salary =1000 #@property 修饰的属性,如果没有加 setter 方法,则为只读属性。此处修改报错:AttributeError: can't set attribute
五.@salary.setter
class Employee: def __init__(self,name,salary): self.name=name self.salary=salary @property def salary666(self): return self.salary @salary666.setter def set(self, salary): if 1000<salary<50000: self.salary=salary else: print("录入错误:薪水在1000-50000这个范围") emp1=Employee("高淇",30000) print(emp1.salary666) emp1.set=500000 print(emp1.salary666)
六.属性和方法命名总结
· _xxx:保护成员,不能用“from module import * ”导入,只有类对象和子类对象能访问这些成员。
· __xxx__:系统定义的特殊成员
· __xxx: 类中的私有成员,只有类对象自己能访问,子类对象也不能访问。(但在类外部可以通过“对象名. _类名__xxx”这种特殊方式访问。Python 不存在严格意义的私有成员)
注:再次强调,方法和属性都遵循上面的规则。
七.面向对象特征
1.封装
隐藏对象的属性和实现细节,只对外提供必要的方法。相当于将“细节封装起来”,只对外暴露“相关调用方法”。通过前面学习的“私有属性、私有方法”的方式,实现“封装”。Python 追求简洁的语法,没有严格的语法级别的“访问控制符”,更多的是依靠程序员自觉实现。
2.继承
class Person:
def __init__(self,name,age):
self.name = name
self.__age = age
def say_age(self):
print(self.name,"的年龄是:",self.__age)
class Student(Person):
def __init__(self,name,age,score):
self.score = score
Person.__init__(self,name,age) #构造函数中包含调用父类构造函数。根据需要,不是必须。 子类并不会自动调用父类的__init__(),我们必须显式的调用它。
s1 = Student("张三",15,85)
s1.say_age()
print(dir(s1))
2.1.类成员的继承和重写
1. 成员继承:子类继承了父类除构造方法之外的所有成员。
2. 方法重写:子类可以重新定义父类中的方法,这样就会覆盖父类的方法,也称为“重写”
class Person:
def __init__(self,name,age):
self.name = name
self.age = age
def say_age(self):
print(self.name,"的年龄是:",self.age)
def say_name(self):
print("我是",self.name)
class Student(Person):
def __init__(self,name,age,score):
self.score = score
Person.__init__(self,name,age) #构造函数中包含调用父类构造函数
def say_score(self):
print(self.name,"的分数是:",self.score)
def say_name(self): #重写父类的方法
print("报告老师,我是",self.name)
s1 = Student("张三",15,85)
s1.say_score()
s1.say_name()
s1.say_age()
执行结果:
张三 的分数是: 85
报告老师,我是 张三
张三 的年龄是: 15
2.2.查看类的继承层次结构
class A:pass
class B(A):pass
class C(B):pass
print(C.mro())
2.3.多重继承
class A:
def aa(self):
print("aa")
class B:
def bb(self):
print("bb")
class C(B,A):
def cc(self):
print("cc")
c = C()
c.cc()
c.bb()
c.aa()
3.多态
多态(polymorphism)是指同一个方法调用由于对象不同可能会产生不同的行为。
关于多态要注意以下 2 点:
1. 多态是方法的多态,属性没有多态。
2. 多态的存在有 2 个必要条件:继承、方法重写。
class Animal:
def shout(self):
print("动物叫了一声")
class Dog(Animal):
def shout(self):
print("小狗,汪汪汪")
class Cat(Animal):
def shout(self):
print("小猫,喵喵喵")
def animalShout(a):
if isinstance(a,Animal):
a.shout() #传入的对象不同,shout 方法对应的实际行为也不同。
animalShout(Dog())
animalShout(Cat())
运行结果:
小狗,汪汪汪
小猫,喵喵喵
八.dir()查看对象属性
class Person:
def __init__(self,name,age):
self.name = name
self.age = age
def say_age(self):
print(self.name,"的年龄是:",self.age)
obj = object()
print(dir(obj))
s2 = Person("高淇",18)
print(dir(s2))
九.重写__str__()方法
object 有一个__str__()方法,用于返回一个对于“对象的描述”,对应于内置函数 str()经常用于 print()方法,帮助我们查看对象的信息。__str__()可以重写。
class Person:
def __init__(self,name,age):
self.name = name
self.__age = age
def __str__(self):
'''将对象转化成一个字符串,一般用于 print 方法'''
return "名字是:{0},年龄是{1}".format(self.name,self.__age)
p = Person("高淇",18)
print(p)
十.MRO()
MRO(Method Resolution Order):方法解析顺序。 我们可以通过 mro()方法获得“类的层次结构”,方法解析顺序也是按照这个“类的层次结构”寻找的。
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()
print(C.mro()) #打印类的层次结构
c.say() #解释器寻找方法是“从左到右”的方式寻找,此时会执行 B 类中的 say()
十一.super()获得父类定义
super()代表父类的定义,不是父类对象。
class A:
def say(self):
print("A: ",self)
print("say AAA")
class B(A):
def say(self):
#A.say(self) 调用父类的 say 方法
super().say() #通过 super()调用父类的方法
print("say BBB")
b = B()
b.say()
十二.特殊方法和运算符重载
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*3)
运算结果:
高淇--高希希
高淇高淇高淇
十三.特殊属性
class A:
pass
class B:
pass
class C(B,A):
def __init__(self,nn):
self.nn = nn
def cc(self):
print("cc")
c = C(3)
print(dir(c))
print(c.__dict__)
print(c.__class__)
print(C.__bases__)
print(C.mro())
print(A.__subclasses__())
运行结果:
['__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__', 'cc', 'nn']
{'nn': 3}
<class '__main__.C'>
(<class '__main__.B'>, <class '__main__.A'>)
[<class '__main__.C'>, <class '__main__.B'>, <class '__main__.A'>, <class 'object'>]
[<class '__main__.C'>]
十四.对象的浅拷贝和深拷贝
·浅拷贝
Python 拷贝一般都是浅拷贝。拷贝时,对象包含的子对象内容不拷贝。因此,源对象 和拷贝对象会引用同一个子对象。
·深拷贝
使用 copy 模块的 deepcopy 函数,递归拷贝对象中包含的子对象。源对象和拷贝对象 所有的子对象也不同
import copy
class MobilePhone:
def __init__(self,cpu,screen):
self.cpu = cpu
self.screen = screen
class CPU:
def calculate(self):
print("计算,算个 12345")
print("CPU 对象:",self)
class Screen:
def show(self):
print("显示一个好看的画面,亮瞎你的钛合金大眼")
print("屏幕对象:",self)
c = CPU()
s = Screen()
m = MobilePhone(c,s)
m.cpu.calculate()
n = m #两个变量,但是指向了同一个对象
print(m,n)
m2 = copy.copy(m) #m2 是新拷贝的另一个手机对象
print(m,m2)
m.cpu.calculate()
m2.cpu.calculate() #m2 和 m 拥有了一样的 cpu 对象和 screen 对象
m3 = copy.deepcopy(m)
m3.cpu.calculate() #m3 和 m 拥有不一样的 cpu 对象和 screen对象
运算结果:
计算,算个 12345
CPU 对象: <__main__.CPU object at 0x00685690>
<__main__.MobilePhone object at 0x00685B50> <__main__.MobilePhone object at 0x00685B50>
<__main__.MobilePhone object at 0x00685B50> <__main__.MobilePhone object at 0x0069B490>
计算,算个 12345
CPU 对象: <__main__.CPU object at 0x00685690>
计算,算个 12345
CPU 对象: <__main__.CPU object at 0x00685690>
计算,算个 12345
CPU 对象: <__main__.CPU object at 0x006A5DB0>
十五.组合
“is-a”关系,我们可以使用“继承”。从而实现子类拥有的父类的方法和属性。“is-a” 关系指的是类似这样的关系:狗是动物,dog is animal。狗类就应该继承动物类。
“has-a”关系,我们可以使用“组合”,也能实现一个类拥有另一个类的方法和属性。” has-a”关系指的是这样的关系:手机拥有 CPU。 MobilePhone has a CPU。
class MobilePhone:
def __init__(self,cpu,screen):
self.cpu = cpu
self.screen = screen
class CPU:
def calculate(self):
print("计算,算个 12345")
class Screen:
def show(self):
print("显示一个好看的画面,亮瞎你的钛合金大眼")
c = CPU()
s = Screen()
m = MobilePhone(c,s)
m.cpu.calculate()
#通过组合,我们也能调用 cpu 对象的方法。相当于手机对象间接拥有了“cpu 的方法”
m.screen.show()
运算结果:
计算,算个 12345
显示一个好看的画面,亮瞎你的钛合金大眼
十六.设计模式_工厂模式实现
class CarFactory:
def createCar(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()
c1 = factory.createCar("奔驰")
c2 = factory.createCar("宝马")
print(c1)
print(c2)
运行结果:
<__main__.Benz object at 0x021C5770>
<__main__.BMW object at 0x021C5790>
十七.设计模式_单例模式实现
单例模式只生成一个实例对象,减少了对系统资源的开销。
class CarFactory: __obj = None #类属性 __init_flag = True def create_car(self,brand): if brand =="奔驰": return Benz() elif brand =="宝马": return BMW() elif brand == "比亚迪": return BYD() else: return "未知品牌,无法创建" def __new__(cls, *args, **kwargs): #创建对象 if cls.__obj ==None: cls.__obj = 1 return object.__new__(cls) def __init__(self): #初始化对象 if CarFactory.__init_flag: print("init CarFactory....") CarFactory.__init_flag = False class Benz: pass class BMW: pass class BYD: pass factory = CarFactory() c1 = factory.create_car("奔驰") c2 = factory.create_car("比亚迪") print(c1) print(c2) factory2 = CarFactory() print(factory) print(factory2)