day16-类和对象

2018-10-27  本文已影响0人  丿Rem丶

一、类的继承

1.基本概念

2.继承方法和内容


二、类的重写

1.添加新的方法

2.重写

3.类中的函数的调用过程

4.添加属性


三、运算符重载

1.定义

2.实现方法

    #  __gt__就是 > 对应的魔法方法
    def __gt__(self, other):
        # self -> 指的是大于符号前面的值, other -> 指的是>符号后面的值
        return self.score > other.score

    # __lt__是 < 对应的魔法方法
    # 注意:gt和lt只需要实现一个就可以了
    def __lt__(self, other):
        return self.score < other.score

    def __add__(self, other):
        return self.score + other.score

四、内存管理机制

1. python中的内存管理

2. 内存管理步骤


五、抽象类和抽象方法

1.抽象类

import abc


class Shape(metaclass=abc.ABCMeta):
    # 声明抽象方法
    @abc.abstractmethod
    def draw(self):
        pass

    @abc.abstractmethod
    def area(self):
        pass

2.抽象方法

class Circle(Shape):
    def draw(self):
        print('画图形')

    def area(self):
        print('面积')
上一篇 下一篇

猜你喜欢

热点阅读