回顾一下类和对象以及继承关系

Posted 叽叽喳喳,嘻嘻哈哈

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了回顾一下类和对象以及继承关系相关的知识,希望对你有一定的参考价值。

# coding:utf-8

import traceback


class A(object):
    __static_field = ‘A_private static field‘  
    public_static_field = ‘A_public_static_field‘  
    start_urls = "A_class_start_urls"
    _one_line = "A_one_line"

    def __init__(self):
        (filename, line_number, function_name, text) = traceback.extract_stack()[-2]
        self.name = text[:text.find(‘=‘)].strip()
self.start_urls = "object_start_urls" if not hasattr(self, "_one_line"): # 类属性中有,也算类实例有 self._one_line = ‘123‘ def __str__(self): return self.name def A_comm_func(self): return "A_comm_func" @classmethod def A_classmethod(cls): return "A_classmethod" @staticmethod def A_staticmethod(): return "A_staticmethod" def repeat(self): return "A_repeat" def ree(self): return self.__re() def __re(self): return "A__re" class B(A): B_public_field = "B_public_field" __B_pri_field = ‘B_pri_field‘ _B_one_line = ‘B_one_line‘ public_static_field = "B_public_static_field" _one_line = "B_one_line" def repeat(self): return "B_repeat" def B_comm_func(self): return "B_comm_func" @classmethod def B_classmethod(cls): return "B_classmethod" @staticmethod def B_staticmethod(): return ‘B_staticmethod‘ a = A() b = B() print "--------------A.dict-----------" print A.__dict__ print "--------------B.dict-----------" print B.__dict__ print "------------ a.__dict__ -----------" print a.__dict__ print "--------- b.dict -----------" print b.__dict__ # print b.A_classmethod() print B.A_classmethod() print A._one_line print a._one_line print b._one_line # 先从自己__dict__找,在去类找,再到类的父类找 print B._one_line # 先从自己找,再去父类找 b._one_line = ‘luanqibazao‘ print b._one_line

  

以上是关于回顾一下类和对象以及继承关系的主要内容,如果未能解决你的问题,请参考以下文章

Javascript 中的类和对象

python-继承以及继承问题和多态

python中类和对象的创建以及继承的实现

Python面向对象编程05:收尾总结一下类和对象

Python面向对象编程05:收尾总结一下类和对象

什么是类?什么是对象?类和对象有什么关系?