python3.x面向对象编程之继承(代码实例)
程序员文章站
2024-01-19 12:24:22
继承
class People():
def __init__(self,name,age):
self.name = name...
继承
class People(): def __init__(self,name,age): self.name = name self.age = age self.friends = [] print("--doens't run ") def eat(self): print("%s is eating..." % self.name) def talk(self): print("%s is talking..." % self.name) def sleep(self): print("%s is sleeping..." % self.name) class Man(People): #Man继承People pass m1 = Man("NiuHanYang",22) m1.eat() #Man继承People,直接调用People里的方法
重构父类方法,给父类的方法增加新功能(先执行父类的方法)
class People(): def __init__(self,name,age): self.name = name self.age = age self.friends = [] print("--doens't run ") def eat(self): print("%s is eating..." % self.name) def talk(self): print("%s is talking..." % self.name) def sleep(self): print("%s is sleeping..." % self.name) class Man(People): #Man继承People def piao(self): print("%s is piaoing..." % self.name) def sleep(self):?#给父类的方法增加新功能? People.sleep(self) print("man is sleeping ") m1 = Man("NiuHanYang",22) #m1.eat() #Man继承People,直接调用People里的方法 m1.sleep()
如果要传入参数给子类,要在子类写构造函数
class Man(People): def __init__(self,name,age,money):?#self,name,age是父类的参数,有多少写多少;money是新子类新添加的参数 People.__init__(self,name,age)? #继承父类 # super(Man,self).__init__(name,age) #新式类写法,作用和上面一样,不用写父类的名字 self.money = money print("%s 一出生就有%s money" %(self.name,self.money))多继承
# class People: 经典类 class People(object): #新式类 #父类1 def __init__(self,name,age): self.name = name self.age = age self.friends = [] print("--doens't run ") def eat(self): print("%s is eating..." % self.name) def talk(self): print("%s is talking..." % self.name) def sleep(self): print("%s is sleeping..." % self.name) class Relation(object): #父类2 def make_friends(self,obj): #w1 print("%s is making friends with %s" % (self.name,obj.name)) class Man(Relation,People): #继承两个父类 def __init__(self,name,age,money): # People.__init__(self,name,age) super(Man,self).__init__(name,age) #新式类写法 self.money = money print("%s 一出生就有%s money" %(self.name,self.money)) def piao(self): print("%s is piaoing ..... 20s....done." % self.name) def sleep(self): People.sleep(self) print("man is sleeping ") class Woman(People,Relation): def get_birth(self): print("%s is born a baby...." % self.name) m1 = Man("NiuHanYang",22,100) w1 = Woman("ChenRonghua",26) # m1.make_friends(w1) # w1.name = "陈三炮" # print(m1.friends[0])
在上面的例子中,父类2没有构造函数,因为
print("%s is making friends with %s" % (self.name,obj.name)) 里,self.name,obj.name在父类1存在,不用重新构造
Python2.x和Python3.x多继承的区别
例如下面的程序:
class A: def __init__(self): print("A") class B(A): pass # def __init__(self): # print("B") class C(A): pass # def __init__(self): # print("C") class D(B,C): pass # def __init__(self): # print("D") obj = D()
经典类和新式类:
class A: #经典类 class A(object): #新式类,在父类添加object效果好
Python3.x经典类和新式类都是统一按广度优先来继承的;而Python2.x经典类是按深度优先继承的,新式类似按广度优先来继承的
多态
一种接口,多种实现。是允许你将父对象设置成为和一个或更多的他的子对象相等的技术,赋值之后,父对象就可以根据当前赋值给它的子对象的特性以不同的方式运作。简单的说,就是一句话:允许将子类类型的指针赋值给父类类型的指针。
作用是接口的重用!为了类在继承和派生的时候,保证使用“家谱”中任何一类的实例的某一属性时的正确调用
class Animal: def __init__(self, name): # Constructor of the class self.name = name def talk(self): # Abstract method, defined by convention only pass #raise NotImplementedError("Subclass must implement abstract method") class Cat(Animal): def talk(self): print('Meow!') class Dog(Animal): def talk(self): print('Woof! Woof!') d = Dog("陈荣华") c = Cat("徐良伟") d.talk() c.talk()
class Animal: def __init__(self, name): # Constructor of the class self.name = name def talk(self): # Abstract method, defined by convention only pass #raise NotImplementedError("Subclass must implement abstract method") @staticmethod def animal_talk(obj): obj.talk() class Cat(Animal): def talk(self): print('Meow!') class Dog(Animal): def talk(self): print('Woof! Woof!') d = Dog("陈荣华") c = Cat("徐良伟") Animal.animal_talk(c)#接口重用 Animal.animal_talk(d)
静态方法
只是名义上归类管理,实际上在静态方法里访问不了类或实例中的任何属性(不用self参数)
上一篇: ZAP介绍-1
下一篇: 你必须知道的互联网协议详解