class Person:
count = 0
def __init__(self,name):
self.name =name
Person.count+=1
def say(self):
print("this is ", self.name)
Person.count-=1
def showCount(self):
if(Person.count ==1):
print("this is just a start")
if(Person.count ==0):
print("this is just an end")
class Man(Person):
def __init__(self,name,age):
Person.__init__(self,name)
self.age = age
def say(self):
Person.say(self)
print("this is from person", self.name)
def showCount(self):
if(Person.count ==2):
print("this is just a start from Man")
if(Person.count ==0):
print("this is just an end from Man")
p = Person("shuofeng")
p.showCount()
p.say()
p.showCount()
man = Man("lxy",26)
man.showCount()
man.say()
man.showCount()
执行结果当然比较简单 就是打印
this is just a start
this is shuofeng
this is just an end
this is lxy
this is from person lxy
this is just an end from Man
说明:这里的count就像是static 声明的一个静态变量一样。
PS:if else while等控制流和java区别不大,continue,break照常使用。