方法_面向对象三大特征_设计模式

1 方法没有重载

  • python中,方法的参数没有确定类型(调用时确定参数的类型)
  • 在类中定义多个重名方法,只有最后一个方法有效

2 方法的动态性

可以给类添加对象
方法和函数都是对象

class Person:                    
    def work(self):              
        print("努力上班!")           
                                 
def play_game(s):                
    print("{}在玩游戏".format(s))    
                                 
def work2(s):                    
    print("hhgz,nlsb")           
                                 
Person.play = play_game          
p = Person()                     
p.work()                         
p.play()         #Person.play(p) 
Person.work=work2                
p.work()        
#运行结果
努力上班!
<__main__.Person object at 0x0000023BBFC7D220>在玩游戏
hhgz,nlsb                 

3 私有属性和私有方法(封装)

没有严格的访问控制限制

  • 两个下划线开头的属性是私有的,其他为公共的
  • 类的内部可以访问私有属性
  • 类外部不能直接访问私有属性
  • 类外部可以通过“_类名__私有属性名”访问
#测试私有属性
class Employee:
    def __init__(self,name,age):
        self.name = name
        self.__age = age     #私有属性

e = Employee('WY',18)
print(e.name)
print(e._Employee__age)
print(dir(e))
#运行结果
WY
18
['_Employee__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']
class Employee:
    __company = 'chengxuyuan'
    def __init__(self,name,age):
        self.name = name
        self.__age = age     #私有属性

    def __work(self):        #私有方法
        print("hhgz,zq")
        print("年龄:{0}".format(self.__age))
        print(Employee.__company)

e = Employee('WY',18)
print(e.name)
print(e._Employee__age)
e._Employee__work()

print(Employee._Employee__company)
#运行结果
WY
18
hhgz,zq
年龄:18
chengxuyuan
chengxuyuan

4 @property装饰器

  • 将方法调用变成“属性调用”
  • get set方法
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("录入错误!薪水在1000-50000这个范围")

'''
    def get_salary(self):
        return self.__salary
    def set_salary(self,salary):
        if 1000<salary<50000:
            self.__salary = salary
        else:
            print("录入错误!薪水在1000-50000这个范围")
'''

emp1 = Employee("wy",30000)
#print(emp1.get_salary())
#emp1.set_salary(20000)
#print(emp1.get_salary())
print(emp1.salary)
emp1.salary = 2000
print(emp1.salary)

5 面向对象三大特征

  • 封装
    隐藏对象的细节,只对外提供必要的方法,私有属性,私有方法
  • 继承
    让子类具有父类的特性,提高了代码的重用性
  • 多态
    同一个方法调用可能会产生不同的行为

5.1 继承

  • 代码复用的重要手段
  • 父类/基类 子类/派生类
  • 一个子类可以有多个直接的父类
  • 没有指定父类,默认父类是object
  • 定义子类时,子类要调父类的构造器
class	子类类名(父类1[,父类2,……]:
	类体
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
#Student继承了Person,Person继承了object
print(Student.mro())

s = Student('wy',18,90)
s.say_age()
print(s.name)
#print(s.age)
print(dir(s))
print(s._Person__age)
#运行结果
年龄,年龄,我也不知道
wy
['_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', 'say_age', 'score']
18

5.2 方法重写

  • 成员继承:子类继承了父类除构造方法之类的所有成员
  • 方法重写:子类可以重新定义父类中的方法,会覆盖父类的方法,也称方法重写
class Person:
    def __init__(self,name,age):
        self.name = name
        self.__age = age   #父类私有属性,子类继承但不能直接用
    def say_age(self):
        print("我的年龄:",self.__age)
    def say_introduce(self):
        print("我的名字是{0}".format(self.name))
class Student(Person):
    def __init__(self,name,age,score):
        Person.__init__(self,name,age) #子类必须显示调用父类的属性,不然不会被调用
        self.score = score
    def say_introduce(self):
        '''重写父类方法'''
        print("老师,我的名字是:{0}".format(self.name)) #对父类的代码做一些修改

s = Student("wy",18,90)
s.say_age()
s.say_introduce()
#运行结果
我的年龄: 18
老师,我的名字是:wy

5.3 object根类__dir()

mro查看类的层次结构

class A:pass
class B(A):pass
class C(B):pass
print(C.mro())
#运行结果
[<class '__main__.C'>, <class '__main__.B'>, <class '__main__.A'>, <class 'object'>]

object是所有类的父类

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('wy',18)
print(dir(s2))
#运行结果
[<class '__main__.C'>, <class '__main__.B'>, <class '__main__.A'>, <class 'object'>]
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
['__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__', 'age', 'name', 'say_age']

5.4 重写__str__()方法

class Person:   #默认继承object
    def __init__(self,name):
        self.name = name
    def __str__(self):
        return '名字是:{0}'.format(self.name)

p = Person('wy')
print(p)
#运行结果
名字是:wy

5.5 多重继承

一个子类可以有多个直接父类,避免使用

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.cc()
c.bb()
c.aa()
#运行结果
cc
bb
aa

如果多个父类有相同名字的方法,在子类没有指定父类名时,解释器按照从左到右的顺序

class A:
    def say(self):
        print('aa')
class B:
    def bb(self):
        print('bb')
    def say(self):
        print('BBB')
#class C(B,A): # 'BBB'
class C(A, B): # 'aa'
    def cc(self):
        print('cc')
c = C()
c.say()

5.6 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()
#运行结果
A: <__main__.B object at 0x0000027A7F53E370>
B: <__main__.B object at 0x0000027A7F53E370>

5.7 多态

同一个方法由于对象不同可能会产生不同的行为
方法多态,继承方法重写

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(English())
#运行结果
中国人用筷子吃饭
英国人用叉子吃饭

5.8 特殊方法和运算符重载

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('wy')
p2 = Person('bv')
x = p1+p2
print(x)
print(p1*3)
#运行结果
wy--bv
wywywy

6 特殊属性

class A:
    pass
class B:
    pass
class C(B,A):
    def __init__(self,nm):
        self.nm = nm
    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.__subclasscheck__) #子类列表
#输出结果
['__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', 'nm']
{'nm': 3}
<class '__main__.C'>
(<class '__main__.B'>, <class '__main__.A'>)
[<class '__main__.C'>, <class '__main__.B'>, <class '__main__.A'>, <class 'object'>]
<built-in method __subclasscheck__ of type object at 0x00000288DA327E20>

7 对象的浅拷贝和深拷贝

浅拷贝:对象包含的子对象不拷贝
深拷贝:包含子对象

class MobilePhone:
    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)
#测试变量赋值
c1 = CPU()
c2 = c1
print(c1)
print(c2)

#测试浅复制
print('测试浅复制......')
import copy
s1 = Screen()
m1 = MobilePhone(c1,s1)
m2 = copy.copy(m1)
print(m1,m1.cpu,m1.screen)
print(m2,m2.cpu,m2.screen)

#测试深复制
print('测试深复制......')
m3 = copy.deepcopy(m1)
print(m1,m1.cpu,m1.screen)
print(m3,m3.cpu,m3.screen)
#运行结果
<__main__.CPU object at 0x0000028D5967D220>
<__main__.CPU object at 0x0000028D5967D220>
测试浅复制......
<__main__.MobilePhone object at 0x0000028D596A6760> <__main__.CPU object at 0x0000028D5967D220> <__main__.Screen object at 0x0000028D5967D370>
<__main__.MobilePhone object at 0x0000028D59576700> <__main__.CPU object at 0x0000028D5967D220> <__main__.Screen object at 0x0000028D5967D370>
测试深复制......
<__main__.MobilePhone object at 0x0000028D596A6760> <__main__.CPU object at 0x0000028D5967D220> <__main__.Screen object at 0x0000028D5967D370>
<__main__.MobilePhone object at 0x0000028D596B2FD0> <__main__.CPU object at 0x0000028D59807E20> <__main__.Screen object at 0x0000028D59807EB0>

8 组合

is-a 关系用继承
has-a 关系用组合

#使用继承实现代码的复用
class A1:
    def say_a1(self):
        print('a1,a1,a1')
class B1(A1):
    pass
b1 = B1()
b1.say_a1()

#同样的效果,使用组合实现代码的复用
class A2:
    def say_a2(self):
        print('a2,a2,a2')
class B2:
    def __init__(self,a):
        self.a = a
a2 = A2()
b2 = B2(a2)
b2.a.say_a2()
#运行结果
a1,a1,a1
a2,a2,a2
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class MobilePhone:
    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)
m = MobilePhone(CPU(),Screen())
m.cpu.calculate()
m.screen.show()
#运行结果
计算
cpu <__main__.CPU object at 0x0000020C7FD4D220>
显示一个好看的画面
screen对象 <__main__.Screen object at 0x0000020C7FD4D370>

9 设计模式

9.1工厂模式

造对象,实现创建者和调用者的分离

class CarFactory:
    def creat_car(self,brand):
        if brand == '奔驰':
            return Bent()
        elif brand == '宝马':
            return BWM()
        elif brand == '比亚迪':
            return BYD()
        else:
            return '未知品牌,无法创建'
class Bent:
    pass
class BWM:
    pass
class BYD:
    pass

factory = CarFactory()
c1 = factory.creat_car('奔驰')
c2 = factory.creat_car('比亚迪')
print(c1)
print(c2)
#运行结果
<__main__.Bent object at 0x000001771300D370>
<__main__.BYD object at 0x0000017713036760>

9.2 单例模式

确保一个类只有一个对象,并且提供一个访问节点

#单例模式
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')
c = MySingleton('cc')
print(a)
print(b)
print(c)
#运行结果
init......
<__main__.MySingleton object at 0x000001D2C2FCD370>
<__main__.MySingleton object at 0x000001D2C2FCD370>
<__main__.MySingleton object at 0x000001D2C2FCD370>
#工厂模式+单例模式
class CarFactory:
    __obj = None  #类属性
    __init_flag = True
    def creat_car(self,brand):
        if brand == '奔驰':
            return Bent()
        elif brand == '宝马':
            return BWM()
        elif brand == '比亚迪':
            return BYD()
        else:
            return '未知品牌,无法创建'
    def __new__(cls, *args, **kwargs):
        if cls.__obj == None:
            cls.__obj = object.__new__(cls)
        return cls.__obj
    def __init__(self):
        if CarFactory.__init_flag:
            print('init CarFactory......')
            CarFactory.__init_flag = False
class Bent:
    pass
class BWM:
    pass
class BYD:
    pass

factory = CarFactory()
c1 = factory.creat_car('奔驰')
c2 = factory.creat_car('比亚迪')
print(c1)
print(c2)

factory2 = CarFactory()
print(factory)
print(factory2)
#运行结果
init CarFactory......
<__main__.Bent object at 0x00000217D6BE6760>
<__main__.BYD object at 0x00000217D6BC8190>
<__main__.CarFactory object at 0x00000217D6BBD370>
<__main__.CarFactory object at 0x00000217D6BBD370>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值