类中可以有方法,类外也可以有函数,其实类就是一种封装,
Python中可以自己定义一个函数,一可以把这个函数在类中封装成一个方法,
其中的属性和方法自然就从父类中继承来了,
要想获得多个类的属性和功能,Python中可以使用接口
#karl doenitz
#coding=utf-8
def speak(sentense):
print sentense speak("I love Lee") class People:#创建一个人类
sentense = "Hello World"
def __init__(self,sentense="你好,世界"):
self.sentense = sentense
def speak(self):
print self.sentense class Animal:#创建一个动物类
def bark(self,voice,name):
print name+voice+"叫" Lihua = People()
Lihua.speak()
Lihua.__init__("我是好孩子")
Lihua.speak()
Lina = People("This world I come!")
Lina.speak()
doudou = Animal()
doudou.bark("汪汪","小狗") class Singer(People):#创建一个歌手类,继承人类
lyric = "歌词"
def sing(self,lyric):
self.lyric = lyric
print self.lyric Caiyilin = Singer()
Caiyilin.sing("日不落")
Caiyilin.speak()
Caiyilin.__init__("我来了")
Caiyilin.speak()
Xusong = Singer("说话")
Xusong.speak()