zoukankan      html  css  js  c++  java
  • python 面向对象编程

    
    node2:/root/python/object#cat fa.py
    class SearchEngineBase(object):
      def main(self,a):
        return a
    node2:/root/python/object#cat ch.py 
    from fa import SearchEngineBase
    class SimpleEngine(SearchEngineBase):
        def __init__(self):
           pass
    x= SimpleEngine()
    print dir(x)
    print x.main('xxyy')
    
    node2:/root/python/object#python ch.py 
    ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'main']
    xxyy
    
    
    
    class Document():
        def __init__(self, title, author, context):
            print('init function called')
            self.title = title
            self.author = author
            self.__context = context # __开头的属性是私有属性
    
        def get_context_length(self):
            return len(self.__context)
    
        def intercept_context(self, length):
            self.__context = self.__context[:length]
    
    harry_potter_book = Document('Harry Potter', 'J. K. Rowling', '... Forever Do not believe any thing is capable of thinking independently ...')
    
    print(harry_potter_book.title)
    print(harry_potter_book.author)
    print(harry_potter_book.get_context_length())
    
    harry_potter_book.intercept_context(10)
    
    print(harry_potter_book.get_context_length())
    
    print(harry_potter_book.__context)
    
    ########## 输出 ##########
    
    init function called
    Harry Potter
    J. K. Rowling
    77
    10
    
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-5-b4d048d75003> in <module>()
         22 print(harry_potter_book.get_context_length())
         23 
    ---> 24 print(harry_potter_book.__context)
    
    AttributeError: 'Document' object has no attribute '__context'
    
    
    
    class Document():
        def __init__(self, title, author, context):
            print('init function called')
            self.title = title
            self.author = author
            self.__context = context # __开头的属性是私有属性
    
        def get_context_length(self):
            return len(self.__context)
    
        def intercept_context(self, length):
            self.__context = self.__context[:length]
    
    harry_potter_book = Document('Harry Potter', 'J. K. Rowling', '... Forever Do not believe any thing is capable of thinking independently ...')
    
    print(harry_potter_book.title)
    print(harry_potter_book.author)
    print(harry_potter_book.get_context_length())
    
    harry_potter_book.intercept_context(10)
    
    print(harry_potter_book.get_context_length())
    
    print(harry_potter_book.__context)
    
    其中,init 表示构造函数,意即一个对象生成时会被自动调用的函数
  • 相关阅读:
    ARP攻击原理与解决
    如何查看数据库各种表oracle
    MyEclipse 8.0注册码
    oracle数据库导入导出
    输出设备已满或不可用, 归档程序无法归档重做日志[oracle解决方法]
    句柄以及对象的比较java
    shutdown immediate 后无法启动实例问题解决
    马云经典语录
    海量数据处理分析_BI
    数据库迁移方案
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13348372.html
Copyright © 2011-2022 走看看