Python笔记-5_下

一、三大特性之类的继承

class Animal:
    type = "哺乳动物"

    def __init__(self,name,age,sex):
        self.name = name
        self.age = age
        self.sex = sex

    def eat(self):
        print('%s is eating...' % self.name)

class Person(Animal):   #括号里是继承父类,父类的名字
    type = '高等哺乳动物'
    def talk(self):
        print('person %s is talking...' % self.name)

    def eat(self):
        print('%s在优雅的吃饭...' % self.name)

class Dog(Animal):
    def eat_shit(self): #子类自己的方法
        print('dog %s is eat shit' % self.name)

p = Person('jack',16,'M')
print(p.type)
p.eat()
p.talk()

d = Dog('憨憨',2,'M')
print(d.type)
d.eat()
d.eat_shit()

父类
Python笔记-5_下
子类
Python笔记-5_下

重写父类方法

父类和子类的方法同时运行

class Animal:
    type = "哺乳动物"

    def __init__(self,name,age,sex):
        self.name = name
        self.age = age
        self.sex = sex

    def eat(self):
        print('%s is eating...' % self.name)

class Person(Animal):   #括号里是继承父类,父类的名字

    type = '高等哺乳动物'
    def talk(self):
        print('person %s is talking...' % self.name)

    def eat(self):
        Animal.eat(self)    #先执行父类的eat
        print('%s在优雅的吃饭...' % self.name)

class Dog(Animal):
    def eat_shit(self): #子类自己的方法
        print('dog %s is eat shit' % self.name)

p = Person('jack',16,'M')
p.eat()

Python笔记-5_下

利用__init__来执行父类的和子类的方法

class Animal:
    type = "哺乳动物"

    def __init__(self,name,age,sex):
        self.name = name
        self.age = age
        self.sex = sex

    def eat(self):
        print('%s is eating...' % self.name)

class Person(Animal):   #括号里是继承父类,父类的名字
    def __init__(self,name,age,sex,hobby):
        Animal.__init__(self,name,age,sex)  
        self.hobby = hobby

    def talk(self):
        print('person %s is talking...' % self.name)

    def eat(self):
        print('%s在优雅的吃饭...' % self.name)

class Dog(Animal):
    def eat_shit(self): #子类自己的方法
        print('dog %s is eat shit' % self.name)

p = Person('jack',16,'M','Security')
print(p.name,p.sex,p.hobby)

Python笔记-5_下

利用super函数
super(自己类的名字).__init__(xxx)

class Animal:
    type = "哺乳动物"

    def __init__(self,name,age,sex):
        self.name = name
        self.age = age
        self.sex = sex

    def eat(self):
        print('%s is eating...' % self.name)

class Person(Animal):   #括号里是继承父类,父类的名字
    def __init__(self,name,age,sex,hobby):
        super(Person,self).__init__(name,age,sex)   #super里是自己类的名字
        self.hobby = hobby  #再加上子类的属性

    def talk(self):
        print('person %s is talking...' % self.name)

    def eat(self):
        print('%s在优雅的吃饭...' % self.name)

class Dog(Animal):
    def eat_shit(self): #子类自己的方法
        print('dog %s is eat shit' % self.name)

p = Person('jack',17,'M','Security')
print(p.name,p.age,p.sex,p.hobby)

Python笔记-5_下

super().init_ 用这种方法的多!!!

class Animal:
    type = "哺乳动物"

    def __init__(self,name,age,sex):
        self.name = name
        self.age = age
        self.sex = sex

    def eat(self):
        print('%s is eating...' % self.name)

class Person(Animal):   #括号里是继承父类,父类的名字
    def __init__(self,name,age,sex,hobby):
        super().__init__(name,age,sex)   #调用的还是父类的方法
        self.hobby = hobby  #再加上子类的属性

    def talk(self):
        print('person %s is talking...' % self.name)

    def eat(self):
        print('%s在优雅的吃饭...' % self.name)

class Dog(Animal):
    def eat_shit(self): #子类自己的方法
        print('dog %s is eat shit' % self.name)

p = Person('jack',18,'M','Security')
print(p.name,p.age,p.sex,p.hobby)

Python笔记-5_下

二、多继承

基础

class Shenxian:
    def fly(self):
        print('神仙飞~...')

class Monkey:
    def eat_peatch(self):
        print('Monkeys like to eat peatch...')


class MonkeyKing(Shenxian,Monkey):
    def play_goden_stick(self):
        print('孙悟空玩金箍棒...')

m = MonkeyKing()
m.play_goden_stick()
m.eat_peatch()
m.fly()

Python笔记-5_下

三、封装

一些内部属性,外部没法读取、修改。
只需要在前面加上__(俩下划线)

class Person(object):
    def __init__(self,name,age):
        self.name = name
        self.age = age
        self.__life_val = 100   #加俩_变成私有属性,外部无法访问

    def get_life_val(self):
        print('生命值:%s' % self.__life_val)
        return self.__life_val

    def __breath(self):     #私有方法,外部也同样无法访问
        print('%s is breathing...' % self.name)

    def got_attack(self):
        self.__life_val -= 20
        self.__breath()
        print('被攻击了 -20血')
        return self.__life_val

p = Person('jack',11)
p.get_life_val()
p.got_attack()

Python笔记-5_下

突破限制,进行访问,修改

class Person(object):
    def __init__(self,name,age):
        self.name = name
        self.age = age
        self.__life_val = 100   #加俩_变成私有属性,外部无法访问

    def get_life_val(self):
        print('生命值:%s' % self.__life_val)
        return self.__life_val

    def __breath(self):     #私有方法
        print('%s is breathing...' % self.name)

    def got_attack(self):
        self.__life_val -= 20
        self.__breath()
        print('被攻击了 -20血')
        return self.__life_val

p = Person('jack',11)
p.get_life_val()
p.got_attack()

#对封装的破解
p._Person__breath()

p._Person__life_val = 10
p.get_life_val()!

上一篇:一脚踩进java之基础篇20——面向对象 (匿名对象、内部类)


下一篇:linux 用户和组操作