• (四)4-2 Python的类 --续


    类的重写
    变量和方法重写

    class parent():
        name = 'parent'
        age = 100
        sex = 'male'
        def __init__(self):
            print('my name is {0}'.format(self.name))
        def get_name(self):
            return self.name
        def get_sex(self):
            return self.sex
    class child(parent):
        name = 'child'
        age = 10
        def __init__(self):
            print('my name is {0}'.format(self.name))
        def hello(self):
            print('hello world')
        def get_name(self):
            print('hello cnblogs')
    
    a = child()
    a.hello()
    print(a.get_name())
    print(a.get_sex())

    运行结果:

    my name is child
    hello world
    hello cnblogs
    None
    male
    male
    class parent(object):
        parent_name = 'parent'
        sex = 'F'
        def __init__(self,address,age):
            self.address = address
            self.age = age
            print('my name is {0}'.format(self.parent_name))
        def get_name(self):
            return self.name
        def get_sex(self):
            return self.sex
    class child(parent):
        child_name = 'child'
        def __init__(self,address,age):
            # parent.__init__(self,address,age)
            super(child,self).__init__(address,age)
            print('my name is {0}'.format(self.child_name))
        def hello(self):
            print('hello world')
        def get_name(self):
            print('hello cnblogs')
    
    a = child('shenzhen',100)
    a.hello()
    print(a.get_name())
    print(a.get_sex())
    print(a.address)
    print(a.age)

    运行结果:

    my name is parent
    my name is child
    hello world
    hello cnblogs
    None
    F
    shenzhen
    100
    class parent(object):
        parent_name = 'parent'
        sex = 'F'
        def __init__(self,address,age):
            self.address = address
            self.age = age
            print('my name is {0}'.format(self.parent_name))
        def get_name(self):
            print('parent ############')
            return self.parent_name
        def get_sex(self):
            return self.sex
    class child(parent):
        child_name = 'child'
        def __init__(self,address,age):
            # parent.__init__(self,address,age)
            super(child,self).__init__(address,age)
            print('my name is {0}'.format(self.child_name))
        def hello(self):
            print('hello world')
        def get_name(self):
            super(child,self).get_name()
            print('hello cnblogs')
    
    
    a = child('shenzhen',10)
    a.hello()
    a.get_name()
    print(a.get_sex())
    print(a.address)
    print(a.age)

    运行结果:

    my name is parent
    my name is child
    hello world
    parent ############
    hello cnblogs
    F
    shenzhen
    10

    8.5 类的私有变量

    在Python中可以通过在属性变量名前加上双下划线定义属性为私有属性

    class A(object):
        _name = 'zhan'
        __sex = 'F'
        def hello(self):
            print(self._name)
            print(self.__sex)
        def get_sex(self):
            return self.__sex
    
    a = A()
    print(a._name)
    # print(a.__sex)
    a.hello()
    print(a.get_sex())

    运行结果:

    zhan
    zhan
    F
    F
  • 相关阅读:
    报表选型应该知道的
    报表工具——开源还是商用
    地图报表怎么做?
    关于报表在移动端展现需你需要知道哪些?
    报表怎样实现滚动的公告效果?
    加速JDBC的快捷方法
    百万级分组大报表开发与呈现
    秒级展现的百万级大清单报表怎么做
    润乾集算器技术文档汇总
    报表使用相关知识及技巧汇总
  • 原文地址:https://www.cnblogs.com/pythonlx/p/7802129.html
Copyright © 2020-2023  润新知