zoukankan      html  css  js  c++  java
  • (四)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
  • 相关阅读:
    「学习笔记」min_25筛
    HNOI2019游记
    【SDOI2017】数字表格
    【APIO2016】烟火表演
    【SCOI2015】小凸想跑步
    java Thread源码分析
    java ThreadGroup源码分析
    bean获取Spring容器
    spring 管理bean
    thinkphp5.0.19 request
  • 原文地址:https://www.cnblogs.com/pythonlx/p/7802129.html
Copyright © 2011-2022 走看看