python inheritance

First, the inheritance feature

 

1 , what is inherited

Inheritance is to make the parent-child relationship between the generated classes and subclasses can have static properties and methods of the parent class.

[ Inheritance is another class of ordinary static properties and methods can be obtained. ( Not all members )] .

In the python , the new class can inherit one or more parent class, the base class may be called the parent class or superclass, the new class is called a derived class, or subclass. Note: Python Inheritance is divided into: single and multiple inheritance.

 

2 , the concept of inheritance in the parent class and subclass

Parent : for the inherited class, called the parent class, also called a base class or superclass.

Subclass : class inheritance other classes, called subclasses, also called the derived class.

 

3 , inheritance role is to improve code reuse

 

Exercise 1 : Create a Dog class and the Cat class, were set name, gender, age properties and definitions EAT () ,

class Animal(object):

    def __init__(self,name,gender,age):

        self.age=age

        self.name=name

        self.gender=gender

    def eat(self):

        print("吃。。。。")

    def sleep(self):

        print("睡。。。。")

    def __str__(self):

        return " Gender {} , Age {}". format (self.name, self.gender, self.age)

class Dog(Animal):

    def bite(self):

        print ( " bark ... ")

xiaotiandog=Dog("哮天犬","",21)

xiaotiandog.eat()

xiaotiandog.sleep()

xiaotiandog.bite ()

result:

eat. . . .

sleep. . . .

Bark. . .

3 , view the inherited parent class

 

Format: Class name .bases

Note: (1) .python3 If a class does not inherit any class, the default inherited object class. We control this new class called class.

(2) .object class is python of ancestors, from all classes are object inherits the class down.

 

4 overwritten Method

 

Subclass and superclass are defined in the same way, we called the method of replication (derived method).

Examples of like calling this method will call the method in their own class of the time.

 

Exercise 1 : Definition of a Person class creates eat () method to define a Student class, created eat () method, requiring students to the cafeteria to eat, the definition of a Boss class created eat () method, the provisions Boss to the hotel to eat.

 

class Person(object):

    def eat(self):

        print ( " eat ")

class Student(Person):

    def eat(self):

        print ( " go to the cafeteria to eat ")

class Boss(Person):

    def eat(self):

        print ( " go to the hotel for dinner .")

 

 

student=Student()

student.eat()

boss=Boss()

boss.eat()

result:

Go to the cafeteria to eat

To the hotel for dinner

5super()

子类和父类有相同的方法,如果子类想调用父类相同的的方法。可以使用 super()方法。

python3中,子类执行父类的方法也可以直接用super方法 ­­­>super() super默认省略了两个参数 第一个参数是类名,第二个参数是self

两个参数可 以省略不传递例如 super(Student,self) super()还可以从类的外部使用 需要传递类名(本类的名称)和对象名

例如 super(Student,student)

 

格式: 父类类名.方法名称(self) 或者 super().方法名称()或者super(本类类名,对象名)

 

6init()方法 ­ 子类继承父类,如果子类不复写父类的init()方法,

 

创建子类对象的时候会自动调用父类init()方法。 ­

 

子类继承父类,如果子类复习了父类的init()方法,

 

创建的子类对象的时候不会再调用父类的init() 方法。

­ 注意:python要求复习父类的init()方法时,需要调用父类的init()   因为存在隐患,

例如父类的初始化方法有参数,子类初始化无参数,子类再调 用父类   的参数的时候就会报错。

class People(object):

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

        print("person------>")

        self.name=name

        self.age=age

        self.sex=sex

    def say(self):

        print("大家好,我是{},今年{}岁,".format())

 

class Student(People):

    def __init__(self,name,sex,age,score):

        super().__init__(name,sex,age)

        self.score=score

    def talk(self):

        print("我是{},成绩{}".format(self.name,self.score))

s=Student("王五","",23,89)

s.talk()

结果:

person------>

我是王五,成绩89

7、派生属性 属性的覆盖(派生属性):子类也可以添加自己新的属性或者在自己这里重新定义 这些属性(不会影响到父类),需要注意的是,一旦重新定义了自己的属性且与 父类重名,那么调用新增的属性时,就以自己为准了(属性的覆盖)

 

8、私有属性私有方法在继承中的表现

父类中的私有方法和私有属性都是不能被子类继承下来的。

 

9、抽象类

之前我们定义了Person类实现了eat()drink()方法, 每种人都会吃喝但是吃喝的地点不同,如果实现了方法体就浪费了。因此我们可 以只定义eat()方法,不实现方法体,这种形式我们可以将方法定义为抽象方 法,具有抽象方法的类就叫做抽象类。 抽象类是一个特殊的类,只能被继承,不能实例化,抽象类中可以有抽象方法和 普通方法。

 

(1) .定义抽象类 定义抽象类需要导入 abc模块。from abc import ABCMeta, abstractmethod

import abc

class Person(metaclass=abc.ABCMeta):

function(){ //技术指标 http://www.kaifx.cn/mt4/kaifx/1732.html

    @abc.abstractclassmethod

    def eat(self):

        pass

    @abc.abstractclassmethod

    def sleep(self):

        pass

    def breathe(self):

        print("呼吸空气")

class Man(Person):

    def eat(self):

        print("吃软饭。。。。")

    def sleep(self):

        print("睡学校。。。。")

class Women(Person):

    def eat(self):

        print("吃硬菜。。。。")


Guess you like

Origin blog.51cto.com/14511863/2459974