面向对象理解(Python)

面向对象

面向对象编程四要素:

  • :具有同样 属性和方法 的对象的集合

  • 对象:类集合中创建出的一个事物,这里是通过class创建生成的某一个object对象

  • 属性:某个静态的特征属性,比如这里的title/author/context

  • 方法:某个动态的属性特征,比如这里的intercept_context()

class Docment():

    def __init__(self,title,author,context):
        self.title = title
        self.author = author
        self.__context = context

    def get_context_len(self):
        return len(self.__context)

    def intercept_context(self,length):
        self.__context = self.__context[:length]
        print(self.__context)


# 创建一个实例对象
harry_potter_book = Docment('Harry Potter Book','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_len())
print(harry_potter_book.intercept_context(10))

_init_()函数

init函数是构造函数,在创建实例对象时会自动调用及执行该函数

__开头的属性,称为私有属性:指不希望在类的函数之外访问和修改的属性

类中的要素:

  • 类属性
    和函数并列地声明或者赋值,称之为类属性,通常记录和类相关的属性,一般用大写表示
    使用方式:类名.属性名

  • 类方法
    使用@classmethod装饰器,接受的第一个参数是cls,常用来实现不同的init构造函数

  • 静态方法:做一些简单的任务,通过@staticmethod装饰器,用于方便测试

class Docment():
    WELCOME_STR = 'Welcome! The context for this book is{}'

    def __init__(self,title,author,context):
        self.title = title
        self.author = author
        self.__context = context

    # 类方法
    @classmethod
    def crete_empty_book(cls,title,author):
        return cls(title=title,author=author,context='nothing')

    # 成员方法
    def get_context_len(self):
        return len(self.__context)

    # 成员方法
    def intercept_context(self,length):
        self.__context = self.__context[:length]
        print(self.__context)

    # 静态方法
    @staticmethod
    def get_welcome(context):
        return Docment.WELCOME_STR.format(context)


# 通过类方法创建一个空书
empty_book = Docment.crete_empty_book('What Every Man Thinks About Apart from Sex', 'Professor Sheridan Simove')
print(empty_book.get_welcome('indeed nothing'))

继承

子类继承父类的属性和方法

class Entity():
    def __init__(self,object_type):
        print('parent class init called')
        self.object_type = object_type

    def get_context_length(self):
        raise Exception('get_context_length not implemented')

    def print_title(self):
        print(self.title)


class Document(Entity):
    def __init__(self,title,author,context):
        print('Docment class init called')
        Entity.__init__(self,'document')  # 调用父类构造函数
        self.title = title
        self.author = author
        self.__context = context

    def get_context_length(self):
        return len(self.__context)


class Video(Entity):
    def __init__(self,title,author,video_length):
        print('Video class init called')
        Entity.__init__(self,'video')  # 调用父类构造函数
        self.title = title
        self.author = author
        self.__video_length = video_length

    def get_context_length(self):
        return self.__video_length

# 创建Document对象
harry_potter_book = Document('Harry Potter(Book)', 'J. K. Rowling', '... Forever Do not believe any thing is capable of thinking independently ...')
# 创建Video对象
harry_potter_movie = Video('Harry Potter(Movie)', 'J. K. Rowling', 120)

# 测试两个类调用父类object_type对象属性
print(harry_potter_book.object_type)
print(harry_potter_movie.object_type)

# 测试父类对象方法
harry_potter_book.print_title()
harry_potter_movie.print_title()

# 测试子类方法
print(harry_potter_book.get_context_length())
print(harry_potter_movie.get_context_length())

# 错误测试调用父类属性和方法
entity = Entity('entity')
print(entity.get_context_length())
print(entity.print_title())

注意

构造函数_init_

每个类都有构造函数,继承类在生成对象的时候,是不会自动调用父类的构造函数的,必须在 init() 函数中显式调用父类的构造函数。

执行顺序是 子类的构造函数 -> 父类的构造函数。

无方法错误提示:

父类 get_context_length() 函数。如果使用 Entity 直接生成对象,调用 get_context_length() 函数,就会 raise error 中断程序的执行

抽象类抽象函数

抽象类:一种特殊的类,生下来就作为父类存在,一旦对象话就会报错,让类继承ABCMeta

抽象函数:特殊的函数,子类必须从写该函数才可以使用,使用@abcstractmethod装饰器

from abc import ABCMeta, abstractmethod
 
class Entity(metaclass=ABCMeta):
    @abstractmethod
    def get_title(self):
        pass
 
    @abstractmethod
    def set_title(self, title):
        pass
 
class Document(Entity):
    def get_title(self):
        return self.title
    
    def set_title(self, title):
        self.title = title
 
document = Document()
document.set_title('Harry Potter')
print(document.get_title())
 
entity = Entity()
 
########## 输出 ##########
 
Harry Potter
 
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-7-266b2aa47bad> in <module>()
     21 print(document.get_title())
     22 
---> 23 entity = Entity()
     24 entity.set_title('Test')
 
TypeError: Can't instantiate abstract class Entity with abstract methods get_title, set_title

制作“搜索引擎”

“搜索引擎”四大核心要素

搜索器:也就是爬虫爬取数据

索引器:将爬取的数据进行处理,得到索引

检索器:对处理后的索引数据进行检索

用户接口:也就是网页或者APP前端页面

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值