• 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 表示构造函数,意即一个对象生成时会被自动调用的函数
  • 相关阅读:
    static和final
    java面向对象白话解说
    方法
    数组
    JDK的安装和java程序的开发步骤以及环境变量配置
    VS2010 根据模型生成数据库 打开edmx.sql文件时 vs出现无响应的解决方案
    js简易写法
    .NET程序性能优化基本要领
    数据采集类
    ASP.NET MVC 3 配置EF自动生成模型
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13348372.html
Copyright © 2020-2023  润新知