Python面向对象编程

from abc import ABCMeta, abstractmethod


class Student(object):
    job = 'study'  # 静态字段

    # 形如__xxx__的都可以用来定制类
    # __slots__限制类的属性
    # __len__()让类作用于len函数
    # __str__返回用户看到的字符串
    # __repr__返回开发者看到的字符串
    # __iter__() 如果一个类想for ... in 循环, 则必须实现一个__iter__方法,该方法返回一个迭代对象
    # __getitem__() 添加[]索引功能
    # __call__()把对象当函数使唤
    # __getattr__(),动态返回一个属性
    # __getitem__(), 如果把对象看成dict,__getitem__()的参数也可能是一个可以作key的object
    # 与之对应的是__setitem__()方法,把对象视作list或dict来对集合赋值
    # __delitem__()方法,用于删除某个元素。

    # 这里的私有变量__idcard在这里需要写成_Student__idcard的格式
    __slots__ = ('name', 'age', 'sex', 'grade', '_Student__idcard', 'id')  # 该类的属性只能是这里面的

    def __call__(self):  # 把对象当函数使唤, 如student1()
        print(self.name)
        print(self.sex)
        print(self.grade)

    @staticmethod
    def sleep():  # 静态方法(没有self, 顶上有@staticmethod)
        print("I am sleeping.")

    def __init__(self, name, id, sex, grade):
        self.name = name  # 类的属性
        self.id = id
        self.sex = sex
        self.grade = grade
        self.__idcard = '3623278424230423794097'  # 类的私有属性, 只有内部可以访问

    def study(self):  # 类的方法
        print("I am studying.")

    def __love(self):  # 类的私有方法, 只有内部可以访问到该方法, 外部无法访问
        print("I am falling love with you.")

    @property
    def teacher(self):  # 把方法当做属性来用(在函数顶上@property), 如 Student1.teacher
        return "王老师"

    def getidcard(self):  # 内部方法访问私有变量
        print(self.__idcard)

    @property
    def idcard(self):  # 内部方法访问私有变量, 并把方法当属性来用, 就可以通过student.idcard来访问__idcard
        return self.__idcard

    @idcard.setter  # 这个idcard.setter的idcard是上一个idcard
    def setidcard(self, newidcard):
        self.__idcard = newidcard

    def love(self):  # 内部方法访问私有方法
        self.__love()

    def __del__(self):
        print("已销毁")


class XueBa(Student):  # 学霸类继承学生类
    __slots__ = ('habit', 'school')  # 子类的slots为自己的加上父类的

    def __init__(self, name, id, sex, grade):
        # Student.__init__(self, name, id, sex, grade)  # 显示继承父类的init方法, 效果等同于下一行
        super(XueBa, self).__init__(name, id, sex, grade)  # 显示继承父类的init方法, 效果等同于上一行

    def study(self):  # 重写study方法
        Student.study(self)  # 先调用父类的study, 然后再次基础上加工
        print("I never stop studying!")


class SuperXueBa(XueBa):  # 超级学霸类继承学霸类
    pass


class GirlXueBa(XueBa):  # 女学霸类继承学霸类
    pass


class SuperGirlXueBa(SuperXueBa, GirlXueBa):  # 超级女学霸类继承超级学霸类和女学霸类, 多重继承
    pass


def main():
    s = Student(1, 2, 3, 4)
    print(s.idcard)
    s.getidcard()
    s.setidcard = 123
    print(s.idcard)
    s()

    x = XueBa(1, 2, 3, 4)
    print(x.grade)
    x.study()

    # 使用元类创建类(type()函数)
    # type(classname, (father1, father2, ...), dict(function1=f1, function2=f2, ...))
    # class的方法名称与函数绑定,这里我们把函数fn绑定到方法名hello上
    def f1(self, name='world'):
        print('hello %s' % name)

    Hello = type('hello', (object,), dict(f=f1))
    a = Hello()
    a.f('what')


class AbstractClass(object):  # 如何写一个接口(抽象类+抽象方法 = 接口)
    __metaclass__ = ABCMeta

    @abstractmethod
    def send(self):
        pass


class B(AbstractClass):
    # AbstractClass是一个抽象类, send是一个抽象方法, 所以继承这个类的话必须定义抽象方法send, 不然会报错
    def send(self):
        print('send')


if __name__ == '__main__':
    main()

猜你喜欢

转载自blog.csdn.net/xiligey1/article/details/80267914