面对对象编程基础之前言

一、前言

1.类、对象、属性、函数

例子

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)

在这里插入图片描述

context为私有属性

  • 类:一群有着相似性的事物的集合,这里对应Python的class
  • 对象:集合中的一个事物,这里对应由class生成某一个object,比如代码中的harry_potter_book
  • 属性:对象的某个静态特征,比如上述代码中的title、author和__context
  • 函数:对象的某个动态能力,比如上述代码中的intercept_context()函数

类,一群有着相同属性和函数的对象的集合

2.类函数、成员函数、静态函数

例子

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

    def __init__(self, title, author, context):
        print('init function called')

        self.title = title
        self.author = author
        self.__context = context

    # 类函数
    @classmethod
    def create_empty_book(cls, title, author):
        return cls(title=title, author=author, context='nothing')

    # 成员函数
    def get_context_length(self):
        return len(self.__context)

    # 静态函数
    @staticmethod
    def get_welcome(context):
        return Document.WELCOME_STR.format(context)


empty_book = Document.create_empty_book('What Every Man Thinks About Apart from Sex', 'Professor Sheridan Simove')
print(empty_book.get_context_length())
print(empty_book.get_welcome('indeed nothing'))

在这里插入图片描述

  • 静态函数

一般而言,静态函数可以用来做一些简单独立的任务,即方便测试,也能优化代码结构。静态函数还可以通过前一行加上@staticmethod来表示,代码中也有相应的示例。

  • 类函数

而函数的第一个参数一般为cls,表示必须传一个类进来。类函数最常用的功能是实现不同的init构造函数,比如上述代码中,create_empty_book类函数,来创造新的书籍对象,其context一定为nothing。这样的代码,比直接构造要清晰一点。类似的,类函数需要装饰器@classmethod来声明。

  • 成员函数

成员函数则是我们最正常的类的函数,不需要任何装饰器声明,第一个参数self代表当前对象的引用,可以通过此函数,来实现想要的查询/修改类的属性等功能。

3.继承

  • 类的继承

指的是一个类既拥有另一个类的特征,也拥有不用于另一类的独特特征

第一个类叫做子类(派生类)

另一个类叫做父类(基类)

例子

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('Document 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

harry_potter_book = Document('Harry Potter(Book)', 'J. K. Rowling',
 '... Forever Do not believe any thing is capable of thinking independently...')

harry_potter_movie = Video('Harry Potter(Movie)', 'J. K. Rowling', 120)

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())

在这里插入图片描述

  • get_context_length()函数

函数重载

子类再写一遍,覆盖原有函数

  • print_title()函数

这个函数在父类里,这便是继承

4.抽象函数和抽象类

例子

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()

在这里插入图片描述

Entity本身是没什么用的,只需定义DocumentVideo的一些基本元素就够。

但是如果生成了一个Entity的对象就要用到抽象类了。

  • 抽象类

是一种特殊的类,作为父类存在,一旦对象化就会报错。

抽象函数定义在抽象类中,子类也必须重载函数才能使用

装饰器@abstractmetgod来表示

5.面向对象编程

  • 面对对象编程

把一组数据结构和处理它们的方法组成对象(object),把相同行为的对象归纳为类(class),通过类的封装(encapsulation)隐藏内部细节,通过继承(inheritance)实现类的特化(specialization)和泛化(generalization),通过多态(polymorphism)实现基于对象类型的动态分派

发布了134 篇原创文章 · 获赞 16 · 访问量 6342

猜你喜欢

转载自blog.csdn.net/weixin_46108954/article/details/104646334