python中对象方法和顶级方法
Python3.5面向对象程序设计之类的继承和多态详解本文实例讲述了python3.5面向对象程序设计之类的继承和多态。分享给大家供大家参考,具体如下:
1、继承的定义
继承是指:可以使用现有类的所有功能,并在无需重新编写原来的类的情况下对这些功能进行扩展。
(1)通过继承创建的新类称为“子类”或“派生类”。
(2)被继承的类称为“基类”、“父类”或“超类”。
继承的过程,就是从一般到特殊的过程。要实现继承,可以通过“继承”(inheritance)和“组合”(composition)来实现。
在某些 oop 语言中,一个子类可以继承多个基类。但是一般情况下,一个子类只能有一个基类,要实现多重继承,可以通过多级继承来实现。
2、继承的分类
继承概念的实现方式主要有2类:实现继承、接口继承。
(1) 实现继承是指使用基类的属性和方法而无需额外编码的能力;
(2)接口继承是指仅使用属性和方法的名称、但是子类必须提供实现的能力(子类重构父类方法);
在考虑使用继承时,有一点需要注意,那就是两个类之间的关系应该是“属于”关系。
抽象类仅定义将由子类创建的一般属性和方法。
oo开发范式大致为:划分对象→抽象类→将类组织成为层次化结构(继承和合成) →用类与实例进行设计和实现几个阶段。
3、示例代码
|
#!/usr/bin/env python # -*- coding:utf-8 -*- # author:zhengzhengliu #类的继承 class people: def __init__( self ,name,age): self .name = name self .age = age def eat( self ): print ( "%s is eating..." % self .name) def sleep( self ): print ( "%s is sleeping..." % self .name) def talk( self ): print ( "%s is talking..." % self .name) class man(people): #继承父类people类 def make_money( self ): print ( "%s is making money..." % self .name) def sleep( self ): people.sleep( self ) #对父类方法的扩展 print ( "man is sleeping..." ) class women(people): def shop( self ): print ( "%s is shopping..." % self .name) m1 = man( "jack" , "20" ) m1.eat() m1.make_money() m1.sleep() w1 = women( "amy" , "25" ) w1.talk() w1.shop() |
运行结果:
jack is eating...
jack is making money...
jack is sleeping...
man is sleeping...
amy is talking...
amy is shopping...
4、子类中对父类的构造函数进行重构两种方法
|
#!/usr/bin/env python # -*- coding:utf-8 -*- # author:zhengzhengliu #类的继承 #class people: #经典类 class people( object ): #新式类 def __init__( self ,name,age): self .name = name self .age = age def eat( self ): print ( "%s is eating..." % self .name) def sleep( self ): print ( "%s is sleeping..." % self .name) def talk( self ): print ( "%s is talking..." % self .name) class man(people): #继承父类people类 def __init__( self ,name,age,money): #people.__init__(self,name,age) #(方法一)对构造函数进行重构、添加父类中没有的属性 super (man, self ).__init__(name,age) #(方法二)利用super对构造函数进行重构(新式类写法) self .money = money print ( "%s have money %s$" % ( self .name, self .money)) def make_money( self ): print ( "%s is making money..." % self .name) def sleep( self ): people.sleep( self ) #对父类方法的扩展 print ( "man is sleeping..." ) class women(people): def shop( self ): print ( "%s is shopping..." % self .name) m1 = man( "jack" , "20" , 10 ) m1.eat() m1.make_money() m1.sleep() w1 = women( "amy" , "25" ) w1.talk() w1.shop() |
运行结果:
j ack have money 10$
jack is eating...
jack is making money...
jack is sleeping...
man is sleeping...
amy is talking...
amy is shopping...
5、多继承方式
|
#!/usr/bin/env python # -*- coding:utf-8 -*- # author:zhengzhengliu #类的继承 #class people: #经典类 class people( object ): #新式类 def __init__( self ,name,age): self .name = name self .age = age self .friends = [] def eat( self ): print ( "%s is eating..." % self .name) def sleep( self ): print ( "%s is sleeping..." % self .name) def talk( self ): print ( "%s is talking..." % self .name) class relationship( object ): def make_friends( self ,obj): print ( "%s is making friends with %s" % ( self .name,obj.name)) self .friends.append(obj) class man(people,relationship): #多继承 def __init__( self ,name,age,money): #people.__init__(self,name,age) #(方法一)对构造函数进行重构、添加父类中没有的属性 super (man, self ).__init__(name,age) #(方法二)利用super对构造函数进行重构(新式类写法) self .money = money print ( "%s have money %s$" % ( self .name, self .money)) def make_money( self ): print ( "%s is making money..." % self .name) def sleep( self ): people.sleep( self ) #对父类方法的扩展 print ( "man is sleeping..." ) class women(people,relationship): #多继承 def shop( self ): print ( "%s is shopping..." % self .name) m1 = man( "jack" , "20" , 10 ) w1 = women( "amy" , "25" ) m1.make_friends(w1) w1.name = "liu" print (m1.friends) |
运行结果:
jack have money 10$
jack is making friends with amy
[<__main__.women object at 0x0057fa30>]
6、新式类与经典类的继承顺序
|
#!/usr/bin/env python # -*- coding:utf-8 -*- # author:zhengzhengliu class a( object ): #新式类 def __init__( self ): print ( "a" ) class b(a): def __init__( self ): print ( "b" ) class c(a): def __init__( self ): print ( "c" ) class d(b,c): def __init__( self ): pass #print("d") obj = d() |
7、继承示例——学校、教师与学生
|
#!/usr/bin/env python # -*- coding:utf-8 -*- # author:zhengzhengliu #继承实例(新式类)——模拟学校、教师与学生 class school( object ): def __init__( self ,name,addr): self .name = name self .addr = addr self .students = [] self .stuffs = [] def enroll( self ,stu_obj): #学生注册 print ( "%s 学员办理注册" % stu_obj.name) self .students.append(stu_obj) def heir( self ,staff_obj): #聘请教师 print ( "聘请教师 %s" % staff_obj.name) self .stuffs.append(staff_obj) class schoolmember( object ): def __init__( self ,name,age,sex): self .name = name self .age = age self .sex = sex def tell( self ): pass class teacher(schoolmember): def __init__( self ,name,age,sex,salary,course): super (teacher, self ).__init__(name,age,sex) self .salary = salary self .course = course def tell( self ): print ( ''' ----- info of teacher:%s ----- name:%s age:%s sex:%s salary:%s course:%s ''' % ( self .name, self .name, self .age, self .sex, self .salary, self .course)) def teach( self ): print ( "%s is teaching course [%s]" % ( self .name, self .course)) class student(schoolmember): def __init__( self ,name,age,sex,stu_id,grade): super (student, self ).__init__(name,age,sex)
|