面向对象2(待补充)

继承

子类可以有父类的功能,也可以有父类的父类的功能

重写

当子类不需要父类的一个方法的实现时,可以重写一个方法,方法名必须和父类相同

子类的方法和父类的方法相同的时候,会先去自己的类中找,找到了就用自己的,找不到去父类找

调用被重写的方法

第一种调用被重写的父类的方法
父类名.方法名(self)
第二种
super().方法名()

私有方法和私有属性并不会被继承

如果调用的是继承的父类的共有方法,可以在这个共有方法里访问父类的私有方法和属性

但是如果在子类中实现了一个共有方法,那么这个方法是不能够调用继承的父类中的私有方法和私有属性

多继承

object是所有类的基类,继承object的类是新式类,python3默认继承,所以都是新式类

多继承查找顺序

c3算法,类名.__mro__   决定调用一个方法时,搜索的顺序,如果在某个类中找到了方法,那么就停止搜索

多态

python崇尚鸭子类型,所谓多态,就是定义时的类型和运行时的类型不一样,此时就称为多态

定义时不知道调谁,执行的时候才知道调谁

class Dog(object):
    def print_self(self):
        print('大家好,我是xxx')

class Xiaobai(Dog):
    def print_self(self):
        print('hello everybody')

def introduce(temp):
    temp.print_self()

dog1=Dog()
dog2=Xiaobai()

introduce(dog1)
introduce(dog2)

实例属性:和具体的某个实例对象有关系

并且一个实例对象和另外一个实例对象是不共享属性的

类属性:类属性所属于类对象

并且多个实例对象之间共享一个类属性

实例属性是对象的属性,如在__init__里的属性就是实例属性

类属性是类的属性,在def外面定义的属性叫做类属性

类方法、实例方法、静态方法

class Game(object):
    #类属性
    num=0
    #实例方法
    def __init__(self):
        #实例属性
        self.name='laowang'
    #类方法
    @classmethod
    def add_num(cls):
        cls.num=100

    #静态方法,和类对象和实例对象都没有关系的方法
    @staticmethod
    def print_menu():
        print('-----')
        print('开始')
        print('结束')
        print('-----')


game=Game()
Game.add_num()#可以通过类名调用类方法
# game.add_num()还可以通过这个类创建出来的对象去调用这个类方法
print(Game.num)
Game.print_menu()#通过类调用静态方法
# game.print_menu()#通过实例对象调用静态方法

在一个类中定义一个方法返回另一个类的对象

class CarStore(object):
    def order(self,money):
        if money>5000:
            return Car()

class Car(object):
    def move(self):
        print('车在移动')
    def music(self):
        print('车在听音乐')
    def stop(self):
        print('车在停止')
car_stone=CarStore()
car=car_stone.order(10000)
car.move()
car.music()
car.stop()

耦合性,关联太强,添加一辆车要去类中修改,删除一辆车也是
class CarStore(object):
    def order(self,car_type):
        if car_type=='索纳塔':
            return Suonata()
        elif car_type=='名图':
            return Mingtu()

class Car(object):
    def move(self):
        print('车在移动')
    def music(self):
        print('车在听音乐')
    def stop(self):
        print('车在停止')

class Mingtu(Car):
    pass
class Suonata(Car):
    print('haha')
car_stone=CarStore()
car=car_stone.order('索纳塔')
car.move()
car.music()
car.stop()


定义一个额外的函数达到解耦的状态

#def select_car_by_type(car_type):
# 功能:返回一个汽车对象的引用
# 参数:需要得到汽车的类型
class CarStore(object):
    def order(self,car_type):
        return select_car_by_type(car_type)
def select_car_by_type(car_type):
    if car_type == '索纳塔':
        return Suonata()
    elif car_type == '名图':
        return Mingtu()

class Car(object):
    def move(self):
        print('车在移动')
    def music(self):
        print('车在听音乐')
    def stop(self):
        print('车在停止')

class Mingtu(Car):
    pass
class Suonata(Car):
    print('haha')
car_stone=CarStore()
car=car_stone.order('索纳塔')
car.move()
car.music()
car.stop()
简单工厂模式
# 定义一个类达到类之间的分离的状态
class CarStore(object):

    def __init__(self):
        self.factory=Factory()

    def order(self,car_type):
        return self.factory.select_car_by_type(car_type)

class Factory(object):

    def select_car_by_type(self,car_type):
        if car_type == '索纳塔':
            return Suonata()
        elif car_type == '名图':
            return Mingtu()

class Car(object):
    def move(self):
        print('车在移动')
    def music(self):
        print('车在听音乐')
    def stop(self):
        print('车在停止')

class Mingtu(Car):
    pass
class Suonata(Car):
    print('haha')
car_stone=CarStore()
car=car_stone.order('索纳塔')
car.move()
car.music()
car.stop()

简单工厂模式,虽然代码可能运行错误,但是大概是这么个意思

# 基类定义流程,但不实现,交给子类实现
class Store(object):
    def select_car(self,car_type):
        pass
    def order(self,car_type):
        return self.select_car(car_type)
class BMWCarStore(Store):
    def select_car(self,car_type):
        return BMWFactory().select_car_by_type(car_type)

bmw_store=BMWCarStore()
bmw=bmw_store.order('123')
# 简单工厂模式
# 定义一个类达到类之间的分离的状态
class CarStore(Store):
    def select_car(self,car_type):
        return Factory().select_car_by_type(car_type)

class BMWFactory(object):
    def select_car_by_type(self,car_type):
        pass


class Factory(object):

    def select_car_by_type(self,car_type):
        if car_type == '索纳塔':
            return Suonata()
        elif car_type == '名图':
            return Mingtu()

class Car(object):
    def move(self):
        print('车在移动')
    def music(self):
        print('车在听音乐')
    def stop(self):
        print('车在停止')

class Mingtu(Car):
    pass
class Suonata(Car):
    print('haha')
car_stone=CarStore()
car=car_stone.order('索纳塔')
car.move()
car.music()
car.stop()

__new__,作用是创建一个对象,__init__负责初始化,两个方法合起来做的事就是构造方法做的事

class Dog(object):
    def __init__(self):
        print('init方法')

    def __new__(cls, *args, **kwargs):
        print('构造方法')
        return object.__new__(cls)#重写new时必须返回父类的new方法,不然创建不了实例对象
    def __del__(self):
        print('del方法')
    def __str__(self):
        print('str方法')
        return '描述信息'
xtq=Dog()
# 做了三件事
# 1、调用__new__方法来创建对象,然后找了一个变量来接收__new__的返回值,这个返回值表示创建出来的对象的引用
# 2、__init__(刚刚创建出来的对象的引用)
# 3、返回对象的引用

单例模式

参考我的博客https://www.cnblogs.com/z-x-y/p/10068512.html

单例模式实现了只能创建一个实例对象,但是能初始化很多次,我们怎么实现只初始化一次对象呢

class Dog(object):

__instance=None
__init_flag=False

def __new__(cls, name):
if not cls.__instance:
cls.__instance=super().__new__(cls)
return cls.__instance

def __init__(self,name):
if Dog.__init_flag==False:
self.name=name
Dog.__init_flag=True

a=Dog('旺财')
print(id(a))
print(a.name)
b=Dog('哮天犬')
print(id(b))
print(b.name)
原文地址:https://www.cnblogs.com/z-x-y/p/10094478.html