美文网首页
2018-12-05

2018-12-05

作者: Karl_2c80 | 来源:发表于2018-12-06 00:07 被阅读0次

    python内部类

    • 在类的内部定义的类

    内部类的实例化方法

    1. 直接使用外部类调用内部类
      object_name = outclass_name.inclass_name()
    2. 先对外部类进行实例化,在实例化内部类
      out_name = outclass_name()
      in_name = out_name.inclass_name()
      in_name.method()

    类的内置方法

    1. __str__() : 给类设置返回值
    #!/usr/bin/python
    #coding:utf8
    
    class MyClass(object):
        name = 'Karl'
        id = 007
        __age = 18
    
        def think(self):
            self.__age = 28
            name = 'John'
            print "My name is %s" %self.name
            print "My age is %s" %self.__age
    
        def __secret(self):
            print "I can tell you a secret!"
    
        def tell(self):
            self.__secret()
    
        def __str__(self):
            return "This is MyClass"        # 只能用return,不能用print
    
    
        @staticmethod
        def test():                 # 注意没带参数self
            print "This is a static func"
    
        @classmethod
        def test2(self):
            print "This is a class func"
    
    #    cm = classmethod(test2)    # 类方法
    
    #    sm = staticmethod(test)    # 静态方法
    
    me = MyClass()
    print me
    
    [root@KARL ~]# python u3.py
    This is MyClass
    
    # 若没有__str__()方法,则打印如下
    [root@KARL ~]# python u3.py
    <__main__.MyClass object at 0x7f25c440fcd0>
    
    1. __init__() : 构造函数(可选),用于初始化类的内部状态
    2. __del__() : 析构函数(可选),用于释放对象占用的资源
      1 #!/usr/bin/python
      2 #coding:utf8
      3
      4 class MyClass(object):
      5     name = 'Karl'
      6     id = 007
      7     __age = 18
      8
      9
     10     def tell(self):
     11         self.__secret()
     12
     13     def __str__(self):
     14         return "This is MyClass"        # 只能用return,不能用print
     15
     16     def __init__(self):
     17         self.name = "Default"
     18         print "Init ..."
     19         print "The name is %s" %self.name
     20
     21     def __del__(self):
     22         print "Del ..."
     23
     24
     25     @staticmethod
     26     def test():                 # 注意没带参数self
     27         print "This is a static func"
     28
     29     @classmethod
     30     def test2(self):
     31         print "This is a class func"
     32
     33 me = MyClass()    # 仅仅实例化类
    
    [root@KARL ~]# python u3.py
    Init ...
    The name is Default
    Del ...
    

    类的继承

    • 子类继承类父类的所有公有属性和方法
    • 继承实现类代码复用
    • class MyClass(ParentClass1 [, ParentClass2, ...])
    • 若父类定义了__init__()方法,子类必须显示调用父类的__init__()方法
    • 若子类需要扩展父类的行为,可添加__init__()方法的参数
    • 子类调用父类的构造函数有以下2种方式
    1. ParentClass.__init__(self [, args ...])
    2. super(MyClass, self).__init__([args ...])
    #!/usr/bin/python
    #coding:utf8
    
    class MyClass(object):
        name = 'Karl'
        id = 007
        __age = 18
    
    
        def __str__(self):
            return "This is MyClass"        # 只能用return,不能用print
    
        def __init__(self, c):
            self.name = "Default"
            print "Init ..."
            print "The name is %s" %self.name
            print "I am %s" %c
    
        def __del__(self):
            print "Del ..."
    
    
        @staticmethod
        def test():                 # 注意没带参数self
            print "This is a static func"
    
        @classmethod
        def test2(self):
            print "This is a class func"
    
    class SonClass(MyClass):
        def __init__(self):
            # MyClass.__init__(self, 'black')
            super(SonClass, self).__init__('black')   # 父类名后必须带有(object)时,才能使用super,注意super的参数用子类名
    
    me = SonClass()   # 仅仅实例化类
    
    [root@KARL ~]# vim u3.py
    [root@KARL ~]# python u3.py
    Init ...
    The name is Default
    I am black
    Del ...
    

    多重继承

    • 多重继承时,若多个父类定义了__init__()方法时,子类只执行第一个类的__init__()方法

    相关文章

      网友评论

          本文标题:2018-12-05

          本文链接:https://www.haomeiwen.com/subject/afqgcqtx.html