第九章 类

#1.
#A:多行注释的方法:使用3个'或者"
#B:__init__()函数类似于类的构造函数,在类对象初始化时候会自动调用
#C:在定义类的成员函数的时候,self形参类似于C++的this指针,必须位于第一个形参的位置,会自动传递
#D:在__init()__函数中,通过self定义的变量可供类内所有方法使用,还可以通过类的任何实例访问
#E:类的成员函数第一个参数都是带self的,不带self的都是静态函数,不能被类的实例访问,可以通过类名访问
#F:给类的成员变量或者成员函数加上前缀__就会使其访问权限变为私有的
#G:给类的成员变量或者成员函数加上前缀__和后缀__会使其在类外能被实例访问,一般不使用这种方法,这是类的一些特殊属性的使用方法
'''
note
note
note
'''
"""
note
note
note
"""
class CTest():
    def __init__(self, value0, value1):
        self.value0 = value0    #定义类的成员变量
        self.__value1 = value1  #类的私有成员变量
        self.__value2 = 0       #赋予类的成员变量初始值
        self.__value3__ = 1
        print("__init__")
    def FunPrint(self):
        print(str(self.value0) + " " + str(self.__value1))
    def FunSet(self, value0, value1):
        self.value0 = value0
        self.__value1 = value1
    def FunTest():              #类的静态成员函数
        print("FunTest")
    def __FunTest1(self):       #类的私有成员函数
        print(self.__value2)
    def FunPrintValue3(self):
        self.__FunTest1()
    def __FunSpecial__(self):
        print("FunSpecial")
Test = CTest('szn', 10)     #__init__
Test.FunPrint()             #szn 10
Test.FunSet(20, 's')
Test.FunPrint()             #20 s
print(Test.value0)          #20
#print(Test.__value1)       #调用会出错
#Test.FunTest()             #调用会出错
CTest.FunTest()             #FunTest
Test.FunPrintValue3()       #0
#Test.__FunTest1()          #调用会出错
Test.__FunSpecial__()       #FunSpecial
print(str(Test.__value3__)) #1
print("")

#2.
#A:子类可以通过super(类名,类实例)来访问子类中覆盖的父类的成员函数
#B:若子类没有定义__init__()函数,则会自动调用父类的__init__()函数,若子类定义了__init()__函数,则不会自动调用父类的__init__()函数
#C:super()函数是一个特殊的函数,将父类和子类关联起来
#D:若不调用父类的__init__()函数,则子类将无法调用父类的成员函数
class CFather():
    def __init__(self, value0 = 0):
        self.value = value0
        print("Father")
    def GetValue(self):
        print(str(self.value) + " Father")

class CChild0(CFather):
    def GetValue(self):     #覆盖了父类的同名函数
        print("CChild0_GetValue")
        super().GetValue()

class CChild1(CFather):
    def __init__(self, value0):
        print("CChild1")

class CChild2(CFather):
    def __init__(self):
        CFather.__init__(self)

class CChild3(CFather):
    def __init__(self):
        super().__init__()


Child0 = CChild0(10)        #Father
Child0.GetValue()           #CChild0_GetValue
10 Father
super(CChild0, Child0).GetValue()   #10 Father
Child1 = CChild1(1)         #CChild1
#Child1.GetValue()          #调用会出错
Child2 = CChild2()          #Father
Child2.GetValue()           #0 Father
Child3 = CChild3()          #Father
Child3.GetValue()           #0 Father

#3.
#A:导入类
#from FileName import ClassName                 #从文件FileName中导入类ClassName   可以直接使用ClassName
#from FileName import ClassName0, className1    #从文件FileName中导入类ClassName0,ClassName1   可以直接使用ClassName ClassName1
#import FileName                                #从文件FileName中导入所有类  使用文件名.ClassName
#from FileName import*                          #从文件FileName导入所有类,不建议使用,可能导致名字冲突    直接使用类名

#4.
#A:python标准库中的OrderedDict是类似于字典的类,其存储是具有顺序的,其存在于模块collections中
from collections import OrderedDict
Test = OrderedDict()
Test['s0'] = '10'
Test['s1'] = 11
print(Test)                 #OrderedDict()([('s0', '10'), ('s1', 11)])

  

原文地址:https://www.cnblogs.com/szn409/p/6511759.html