day17-2 继承

继承:作用是为了减少代码

 1 class People:
 2 
 3     def __init__(self,name,age):
 4         self.name = name
 5         self,age = age
 6 
 7     def eat(self):
 8         print("%s is eating" % self.name)
 9 
10     def talk(self):
11         print("%s is talking" % self.name)
12 
13     def sleeping(self):
14         print("%s is sleeping" % self.name)
15 
16 class Man(People):  # 继承父类
17 
18     def fly(self):
19         print('%s男人' % self.name)
20 
21     # def sleeping(self):
22     #     print("the man is sleeping")  # 这个会直接覆盖掉父类的方法
23 
24     def sleeping(self):
25         People.sleeping(self)   # 先执行父类的方法
26         print("%s 已经熟睡" % self.name)
27 
28 m1 = Man('rich','11')
29 m1.eat()
30 m1.fly()
31 m1.sleeping()
32 
33 class Woman(People):
34 
35     def get_birth(self):
36         print("%s is make a children.... " % self.name)
37 
38 w1 = Woman('yang','22')
39 w1.get_birth()
40 
41 # 
42 # rich is eating
43 # rich男人
44 # rich is sleeping
45 # rich 已经熟睡
46 # yang is make children.... 

 多继承

class People(object):  # 新式类
# class People: #经典类

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

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

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

    def sleeping(self):
        print("%s is sleeping" % self.name)

class Relation(object):

    def make_friends(self,obj):
        print("%s is making friends with %s" %(self.name,obj.name))


class Man(Relation,People):  # 继承父类
     # 重构父类
     # 给Man传入新的属性
    def __init__(self,name,age,job):
        # People.__init__(self,name,age)
        super(Man,self).__init__(name,age)  # 与上面一行代码作用一样,但是如果继承的类做了更改,这种方法不需要修改,同时如果多继承,上面那段代码需要在每一个继承类的地方添加
        self.job = job
    def fly(self):
        print('%s男人' % self.name)

    # def sleeping(self):
    #     print("the man is sleeping")  # 这个会直接覆盖掉父类的方法

    def sleeping(self):
        People.sleeping(self)   # 先执行父类的方法
        print("%s 已经熟睡" % self.name)

m1 = Man('rich','11','IT')
print(m1.job)
m1.eat()
m1.fly()
m1.sleeping()

class Woman(People):

    def get_birth(self):
        print("%s is make a children.... " % self.name)

w1 = Woman('yang','22')
w1.get_birth()
m1.make_friends(w1)   # 多继承   为什么要进行多继承,
#
# IT
# rich is eating
# rich男人
# rich is sleeping
# rich 已经熟睡
# yang is make a children.... 
# rich is making friends with yang

新式类和经典类:

新式类和经典类的区别主要体现在多继承的继承问题:

在python 2 上经典类是按深度优先来继承的,新式类是按广度优先继承的

python 3 上经典类和新式类都是按广度优先类继承的

 

继承实例详解

  1 #描述一个学校,老师,学生的关系
  2 
  3 class School_own(object):\
  4 
  5     def __init__(self,name,addr):
  6         self.name = name
  7         self.addr = addr
  8         self.students = []
  9         self.teachers = []
 10 
 11     def enroll(self,stu_obj):
 12         self.students.append(stu_obj)
 13         print("%s 为学生办理注册手续" % stu_obj.name)
 14 
 15     def hire(self,staff_obj):
 16         self.teachers.append(staff_obj)
 17 
 18 
 19 class SchoolMember(object):
 20 
 21     def __init__(self,name,age,sex):
 22         self.name = name
 23         self.age = age
 24         self.sex = sex
 25 
 26 class Teacher(SchoolMember):
 27     def __init__(self,name,age,sex,salary,course):
 28         super(Teacher,self).__init__(name,age,sex)
 29         self.salary = salary
 30         self.course = course
 31 
 32     def tell(self):
 33         print('''
 34         -------------info of Teacher %s-------
 35         Name:%s
 36         Age:%s
 37         sex:%s
 38         Salary:%s
 39         Course:%s
 40         ''' %(self.name,self.name,self.age,self.sex,self.salary,self.course))
 41 
 42     def teach(self):
 43         print("%s is teacheing course [%s]" %(self.name,self.course))
 44 
 45 
 46 class Stduent(SchoolMember):
 47     def __init__(self,name,age,sex,stu_id,grade):
 48         super(Stduent,self).__init__(name,age,sex)
 49         self.stu_id = stu_id
 50         self.grade = grade
 51 
 52     def tell(self):
 53         print('''
 54         -------------info of Student %s-------
 55         Name:%s
 56         Age:%s
 57         sex:%s
 58         Stu_id:%s
 59         Grade:%s
 60         ''' %(self.name,self.name,self.age,self.sex,self.stu_id,self.grade))
 61 
 62     def pay_tuition(self,amount):
 63         print("%s has paid tuition $%s" %(self.name,amount))
 64 
 65 school = School_own("qhgdx",'yibin')
 66 
 67 t1 = Teacher("02",'22','m','100000','shua')
 68 t2 = Teacher("houyali",'99','w','10','cui')
 69 
 70 s1 = Stduent("yang",66,"M",1510,"python")
 71 s2 = Stduent("rich",66,"M",1510,"python")
 72 
 73 
 74 t1.tell()
 75 s1.tell()
 76 school.enroll(s1)
 77 school.enroll(s2)
 78 school.hire(t1)
 79 print(school.students[0].name)
 80 print(school.teachers[0].name)
 81 school.teachers[0].teach()
 82 
 83 for stu in school.students:
 84     stu.pay_tuition(5000)
 85 
 86 #     -------------info
 87 #     of
 88 #     Teacher
 89 #     02 - ------
 90 #     Name: 02
 91 #     Age: 22
 92 #     sex: m
 93 #     Salary: 100000
 94 #     Course: shua
 95 #
 96 #     -------------info
 97 #     of
 98 #     Student
 99 #     yang - ------
100 #     Name: yang
101 #     Age: 66
102 #     sex: M
103 #     Stu_id: 1510
104 #     Grade: python
105 #
106 # yang
107 # 为学生办理注册手续
108 # rich
109 # 为学生办理注册手续
110 # yang
111 # 02
112 # 02 is teacheing
113 # course[shua]
114 # yang
115 # has
116 # paid
117 # tuition $5000
118 # rich
119 # has
120 # paid
121 # tuition $5000

 

posted @ 2018-10-21 11:20  杨fj  阅读(94)  评论(0编辑  收藏  举报