Python之类的继承

类的继承代码

#Author Kang

class Person(object):                    #父类

    countPerson = 0                 #公有属性

    def __init__(self,name,sex):         #类的初始化
        self.Name = name
        self.Sex = sex
        Person.countPerson += 1      #统计注册人数,必须要这样写,否则无法形成统计

    def walking(self):                  #类的方法
        print("%s is walking"%self.Name)

class BlackPerson(Person):                     #子类
    def __init__(self,name,sex,language):      #初始化函数,先继续父类,再重构自己
        Person.__init__(self,name,sex)
        self.Language = language

    def talking(self):                         #子类的方法
        print("%s is talking,Lanaguage is %s"%(self.Name,self.Language))

    def walking(self):                        #重构父类的方法
        print("我是重构了父类的方法,walking.......!!!!!")

b2 = BlackPerson("kang","F","chiness")
b1 = BlackPerson("Jack","F","English")
b1.talking()
b1.walking()
print("当前人数:",Person.countPerson)

结果:
Jack is talking,Lanaguage is English
我是重构了父类的方法,walking.......!!!!!
当前人数: 2

本文名称:Python之类的继承
网站路径:http://pwwzsj.com/article/giiggc.html