直播代码,python的隐式继承

#!/usr/bin/env python 
# -*- coding:utf-8 -*-
 
class Modle:
    __name = "DNN"
 
    def __init__(self, name):
        self.__name = name
 
    def print_name(self):
        print(self.__name)
 
 
    @classmethod             #类方法不管后面被继承了多少次,它始终绑定在类Modle上
    def print_cls_name(cls):
        print(cls.__name)
 
class CNNModle(Modle):  # 继承
    __name = "CNN"
 
    def __init__(self,name):
        self.__name = name
 
 
class CNNModleBp(CNNModle):
    __name = "CNNModleBp"
 
 
def main():
    cnnmodle = CNNModle("Lenet") # __init__(self, name),self指代cnnmodle,name是Lenet
    cnnmodle.print_name()
    cnnmodle.print_cls_name()    # 这里的name是Modle的name 而不是CNNModle的name
                                 # 否则print的结果应该是CNN,实际打印的是DNN
    cnnmodlebp = CNNModleBp("net")
    cnnmodlebp.print_name()
    cnnmodlebp.print_cls_name()
 
if __name__ == '__main__':
    main()
 
 

你可能感兴趣的:(技术类,python)