面向对象——继承

概念

面向对象编程 (OOP) 语言的一个主要功能就是“继承”。继承是指这样一种能力:它可以使用现有类的所有功能,并在无需重新编写原来的类的情况下对这些功能进行扩展。当同一个类的对象有共同的属性也有不同的属性时,我们就弄一个父类弄多个子类,子类继承父类的属性,且子类又有自己特有的属性。是面向对象(oop)的主要功能

被继承的类称为 “父类”或者“基类”

通过继承创建的新类称为“子类”或者“派生类”

继承概念的实现方式主要有2类:实现继承、接口继承。

  1. 实现继承是指使用基类的属性和方法而无需额外编码的能力。
  2. 接口继承是指仅使用属性和方法的名称、但是子类必须提供实现的能力(子类重构爹类方法)。

 子类继承父类方法如下例子:

说明:在子类名的括号中写入需要继承的类名(父类的类名)即可

 1 class Person(object):
 2     def talk(self):
 3         print("people is taking....ll")
 4 
 5 
 6 class blackpeople(Person):
 7     def walk(self):
 8         print("blackpeople in walking...")
 9 
10 p1 = blackpeople()
11 p1.talk()  #继承了父类,所以talk()方法可以被调用 
12 p1.walk()
13 结果:
14 people is taking....ll
15 blackpeople in walking...

若父类里有构造函数含有参数,子类继承父类时应把参数传递进去,否则会报错

class Person(object):
    def __init__(self,name,age):
        self.name = name
        self.age = age
    def talk(self):
        print("people is taking....ll")


class blackpeople(Person):
    def walk(self):
        print("blackpeople in walking...")

p1 = blackpeople()
p1.talk()
p1.walk()

报错:
Traceback (most recent call last):
  File "E:/python_3.5/stu/继承模块.py", line 16, in <module>
    p1 = blackpeople()
TypeError: __init__() missing 2 required positional arguments: 'name' and 'age'

将参数传递进去

 1 class Person(object):
 2     def __init__(self,name,age):
 3         self.name = name
 4         self.age = age
 5     def talk(self):
 6         print("people is taking....ll")
 7 
 8 
 9 class blackpeople(Person):
10     def walk(self):
11         print("blackpeople in walking...")
12 
13 p1 = blackpeople("HiHi",11)
14 p1.talk()
15 p1.walk()

若想要继承父类的构造函数,同时又要有自己的属性(功能),做法是先继承再构造

 1 class People(object):
 2     def __init__(self, name, age):
 3         self.name = name
 4         self.age = age
 5         self.height = "normal"
 6 
 7     def talk(self):
 8         print("People is talking...")
 9 
10 
11 class BlackPelple(People):
12     def __init__(self, name, age, sex):  # 定义时需要传入父类的属性名
13         People.__init__(self, name, age)  # 继承父类的构造方法,也可以写成:super(BlackPerson,self).__init__(name,age)
14         self.sex = sex  # 定义子类本身属性
15         print(self.name, self.age, self.height)
16 
17     def walk(self):
18         print("the people is walking...")
19 
20 p = BlackPelple("wxq", 22 ,"")  # 创建一个实例
21 print(p.sex)
22 p.talk()
23 p.walk()
24 
25 结果:
26 wxq 22 normal
27 28 People is talking...
29 the people is walking...

练习 

类继承联系

#!/usr/bin/env python
# -*- coding:utf-8 -*-

class SchoolMember(object):
    """学校成员基类"""
    member = 0
    def __init__(self, name, age, sex):
        self.name = name
        self.age = age
        self.sex = sex

    def enrrol(self):
        print("just enrrol new school member")
        SchoolMember.member += 1

    def tell(self):
        print("------info:%s-----"%(self.name))
        for k, v in self.__dict__.items():
            print("	", k, v)

        print("------end------")

    def __del__(self):
        print("开除了...%s"%(self.name))
        SchoolMember.member -= 1


class Teacher(SchoolMember):
    """讲师类"""
    def __init__(self, name, age, sex, salary, course):
        SchoolMember.__init__(self, name, age, sex)
        self.salary = salary
        self.course = course

    def teaching(self):
        """讲课方法"""
        print("Teacher %s is teaching %s "%(self.name, self.course))

class Student(SchoolMember):
    """学生类"""
    def __init__(self, name, age, sex, tuition, course):
        SchoolMember.__init__(self, name, age, sex)
     # 或者
     #
super(Student, self).__init__(name, age, sex)
        self.tuition = tuition
        self.course = course

    def pay_tuition(self, amount):
        print("student %s has just paied %s"%(self.name, amount))
        self.amount += amount

t1 = Teacher("Lily", 22, "", 6666, "Python")
s1 = Student("A1", 12, "", "PYS15", 1 )
s2 = Student("A2", 12, "", "PYS14", 1 )
print(SchoolMember.member)
t1.tell()
s1.tell()
s2.tell()



结果:
0
------info:Lily-----
     salary 6666
     age 22
     sex 女
     course Python
     name Lily
------end------
------info:A1-----
     tuition PYS15
     age 12
     sex 男
     course 1
     name A1
------end------
------info:A2-----
     tuition PYS14
     age 12
     sex 男
     course 1
     name A2
------end------
开除了...Lily
开除了...A1
开除了...A2
原文地址:https://www.cnblogs.com/wengxq/p/7420184.html