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
  • 相关阅读:
    数据类型(列类型-小数型)
    Python全栈开发之2、数据类型-数值、字符串、列表、字典、元组和文件处理
    Python全栈开发之3、深浅拷贝、变量和函数、递归、函数式编程、内置函数
    Python全栈开发之1、python基础初识介绍以及安装
    数据类型(列类型)
    中文数据问题
    SQL--数据--基本操作
    SQL--数据表--基本操作
    SQL--数据库--基本操作
    Mysql数据库
  • 原文地址:https://www.cnblogs.com/pythonlx/p/7802129.html
Copyright © 2011-2022 走看看