#!/usr/bin/env python
# -*- coding:utf-8 -*-
class Modle:
__name = "DNN"
def __init__(self, name): # 只需要传入name
self.__name = name
def print_name(self):
print(self.__name)
@classmethod
def print_cls_name(cls):
print(cls.__name)
class CNNModle(Modle):
__name = "CNN"
def __init__(self,name,layer_num): # 既需要传入name,还需要传入层数layer_num
Modle.__init__(self,name) #这行非常关键,没有这行代码就会报错,显式调用的意思
self.__layer_num = layer_num
def print_cls_name(self):
print(self.__layer_num)
def main():
cnnmodle = CNNModle("Lenet",5)
cnnmodle.print_name()
cnnmodle.print_cls_name()
if __name__ == '__main__':
main()
输出:
要点:如果子类中定义了__init__()函数,必须显示执行父类的初始化