继承

#单继承
__Author__ = 'Cairo'

zero, one, two, three, fous, five, six, seven, eight, nine = range(10)

class Person():
    def __init__(self,name,age,sex,money):
        self.name = name
        self.age = age
        self.sex = sex
        ##如果将变量私有化以后,子类就不能够继承了,如果需要继承只能通过
        # @property装饰器来开通一个接口,这个接口只能在父类中开通
        # @property装饰器的作用 :就是把一个方法变成一个属性来调用
        # 使用方法:把 @ property添加给getter,@属性名.setter装饰器
        # 添加给setter。@property装饰器的优点 :调用起来更加简单。
        self.__money = money
    @property
    def money(self):
        print(self.__money)
    @money.setter
    def money(self,money):
        return self.__money


    def Name(self):
        print("我叫%s。。"%self.name)
    def eat(self,food):
        '''
        :param food: 通过外界这个函数传递的变量,比如Student("Cairo",18,"boy",10000).eat("苹果")
        :return:
        '''
        print("吃%s"%food)
        food = 'QQ'
        return food

class Student(Person):
    pass

# food = "苹果"
# Student("Cairo",18,"boy",10000).Name()
# Student("Cairo",18,"boy",10000).eat(food)
# Student("Cairo",18,"boy",10000).money###属性调用的时候是不用小括号的
# print(Student("Cairo",18,"boy",10000).eat(food))

'''
单继承
总结:
object是一切的基类(父类)
1.子类对象可以直接访问父类中未私有的变量
2.子类对象可以调用父类中的所有方法(包括静态方法和类方法)
3.父类对象不能访问子类特有的属性或者方法。

优点:
1.简化代码
2.提高代码的可维护性
3提高代码的安全性
缺点:
增大了代码的耦合性。若是修改了父类,所有的子类都会跟着改变

当子类中定义了属于自己的属性的时候,这时候不单单是需要初始化自己的属性
也是需要对父类的属性初始化
初始化的方式有:
#super (当前类名,self).__init__(参数列表)
例如:super(Cat,self).__init__(参数1,参数2)
#父类名.__init__(父类的参数列表)
例如:Animal.__init__(self,参数1,参数2)
'''
#多继承
__Author__ = 'Cairo'

zero, one, two, three, fous, five, six, seven, eight, nine = range(10)

'''
多继承:一个类继承多个父类,就叫多继承。
'''
class Father():
    def __init__(self,money):
        self.money = money
    def makeMoney(self):
        print("赚钱。。。。")
class Mother():
    def __init__(self,faceValue):
        self.faceValue = faceValue
    def spendMoney(self):
        print("霍霍钱儿。。。")
class Son(Father,Mother):
    def __init__(self,money,faceValue):
        # 在多继承的时候super这个关键字不能初始化两个父类中的属性
        # super(Son, self).__init__(money,faceValue)
    #     只能使用:
        Father.__init__(self,money)
        Mother.__init__(self,faceValue)
    pass
son = Son(10000000,100)
print(son.money)
#多肽
__Author__ = 'Cairo'

zero, one, two, three, fous, five, six, seven, eight, nine = range(10)

'''
多肽:一类事物的多种形态[依赖于继承]

多肽:在代码运行的过程中,若要添加Animal的一些子类,我们
的run方法是不用修改的
'''
class Animal():
    def __init__(self,name):
        self.name = name
    def run(self):
        print(self.name +"Animal runing")
class Dog(Animal):
    pass
    # def run(self):
    #     print("Dog running")
class Cat(Animal):
    pass
    # def run(self):
    #     print("cat running")

dog = Dog("gou")
dog.run()

cat = Cat("mao")
cat.run()


# 数据类型判断
print(isinstance(dog,Dog))
isinstance(cat,Cat)
isinstance(dog,Animal)
isinstance(cat,Animal)
##函数重写
__Author__ = 'Cairo'

zero, one, two, three, fous, five, six, seven, eight, nine = range(10)

def __str__(self):
    return ""
def __repr__(self):
    __str__ = self.__repr__
    return ""
'''

函数重写:
__str__()函数在打印对象的时候自动调用,是方便用户查看
__repr__()函数给解释器看的,若有的类中已经重写了__repr__()
但是没有重写了__str__(),这个时候我们只需要在__repr__()函数
中添加一行__str__ = self.__repr__即可。

'''
以上内容作为课堂笔记,如有雷同,请联系于我
原文地址:https://www.cnblogs.com/ArtisticMonk/p/9117056.html