class Song(object):#definition def __init__(self, lyrics): self.lyrics = lyrics#add attribution def sing_me_a_song(self):#methods for line in self.lyrics: print line happy_bday = Song(["Happy birthday to you", "I don't want to get sued", "So I'll stop right there"])#object1 bulls_on_parade = Song(["They rally around the family", "With pockets full of shells"])#object2 happy_bday.sing_me_a_song()#call function bulls_on_parade.sing_me_a_song()
class Parent(object): def implicit(self): print "PARENT implicit()" class Child(Parent): pass dad = Parent() son = Child() dad.implicit() son.implicit()
void f(string str)//输出字符串str 1次 { cout<<str<<endl; } void f(string str,int times)//输出字符串 times次 { for(int i=0;i<times;i++) { cout<<str<<endl; } }
<span style="font-size:18px;">def f(str,times=1): print str*times f('sssss') f('sssss',10)</span>
class Parent(object): def override(self): print "PARENT override()" class Child(Parent): def override(self): print "CHILD override()" dad = Parent() son = Child() dad.override() son.override()
class Parent(object): def altered(self): print "PARENT altered()" class Child(Parent): def altered(self): print "CHILD, BEFORE PARENT altered()" super(Child, self).altered() print "CHILD, AFTER PARENT altered()" dad = Parent() son = Child() dad.altered() son.altered()
class Child(Parent): def __init__(self, stuff): self.stuff = stuff super(Child, self).__init__()
一入python深似海--class,布布扣,bubuko.com
原文:http://blog.csdn.net/u010367506/article/details/30845141