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
  • 相关阅读:
    解决vs 编译的bug“请检查是否是磁盘空间不足、路径无效或权限不够”
    lua 使用正则表达式分割字符串
    cocos2dx通过ndk编译c++库
    通过luac编译lua脚本
    redis的一个bug
    将文件转成16进制过程
    fiddler 模拟发送post请求
    cocostudio的bug(1)
    Eclipse+Tomcat搭建jsp服务器
    iOS本地推送与远程推送
  • 原文地址:https://www.cnblogs.com/pythonlx/p/7802129.html
Copyright © 2011-2022 走看看