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
  • 相关阅读:
    三、sersync+rsync实现服务器文件实时同步
    二、Linux实时同步软件之inotify
    一、rsync基础原理
    Samba实战
    DHCP企业实战
    NTP服务器企业实战
    Vsftpd服务器原理及部署
    Python的五大数据类型的作用、定义方式、使用方法,两种交互式方式,格式化输出的三种方式练习。
    pycharm快捷键,变量,字符串,类型的操作方法
    python基础归纳练习 python两种方式,垃圾回收机制,小数整池,数字类型,字符串类型。
  • 原文地址:https://www.cnblogs.com/pythonlx/p/7802129.html
Copyright © 2011-2022 走看看