day14面向对象(2)

2018-08-02  本文已影响0人  KingJX

01-重写

继承后,子类可以拥有除父类继承的内容以外的其他的内容

先看当前类是否存在这个方法,没有才看父类有没有这个方法;
如果父类没有就看父类的父类有没有,直到找到基类(object)为止

class Animal(object):
    """动物类"""
    def __init__(self):
        self.age = 0
        self.color = ''

    def eat(self):
        print('吃东西')

    def shout(self):
        print('叫唤')

    @classmethod
    def get_number(cls):
        return 100


class Dog(Animal):
    """狗类"""
    def look_after(self):
        print('看家')

    #  重写父类的shout
    def shout(self):
        print('汪汪汪~')

    # 重写父类eat方法
    def eat(self):
        # 保留父类eat的功能
        super().eat()
        print('吃骨头')

    @classmethod
    def get_number(cls):
        # 保留父类的类方法的功能的时候,还是super().类方法
        print(super().get_number())


if __name__ == '__main__':
    dog = Dog()
    dog.age = 3
    print(dog.color)
    dog.shout()
    dog.look_after()
    dog.eat()

    an = Animal()
    # 继承后,父类不能使用在子类中添加的属性和方法
    # an.look_after()
    an.eat()

    Dog.get_number()

输出结果:
汪汪汪~
看家
吃东西
吃骨头
吃东西
100

02-添加属性

class Person(object):
    """人类"""
    def __init__(self, name, age=0, sex=''):
        self.name = name
        self.age = age
        self.sex = sex

    def eat(self):
        print('人在吃饭')


class Staff(Person):
    # init方法的参数:保证在创建对象的时候就可以给某些属性赋值
    def __init__(self, name='', age=0, salary=0):
        super().__init__(name, age)
        self.salary = salary

    def eat(self):
        print('员工在吃饭')

class Scientist(Person):
    def eat(self):
        print('科学家在吃饭')


if __name__ == '__main__':
    p1 = Person('李四',sex='女')
    p1.eat()

    s1 = Staff(age=18)
    s1.sex = '男'
    print(s1.name)
    s1.salary = 10000
    print(s1.salary)
    s1.eat()


# 练习:
"""
声明人类,有属性,名字、年龄、性别、身高
要求创建人的对象的时候可以给名字、性别、年龄赋初值

再创建学生类继承自人类,拥有人类的所有的属性,再添加学号、成绩、电话属性
要求创建学生对象的时候可以给名字、年龄和电话赋初值
"""
class Person2:
    """人类"""
    a = 10

    def __init__(self, name, sex, age):
        self.name = name
        self.age = age
        self.sex = sex
        self.height = 0


class Student(Person2):
    """学生类"""
    b = 100

    def __init__(self, name='', age=0, tel='00'):
        super().__init__(name, age=age, sex='女')
        self.study_id = '00'
        self.score = 0
        self.tel = tel


stu = Student('李四')
stu.sex = '男'
print(stu.age)

p1 = Person2('张三', '男', 18)

输出结果:
人在吃饭

10000
员工在吃饭
0

03-运算符的重载

class Student:

    # def run(self):
    #     print('run')

    def run(self,name):
        print("%s ran" % name)

"""
2.运算符重载(重新定义运算符运算的过程)
<,>
大于和小于符号只需要重载其中的一个,另一个的结果,直接是重载的结果取反
+,-
"""

class Student2:
    def __init__(self, name='', age=0, height=0):
        self.name = name
        self.age = age
        self.height = height

    # 重载:>
    """
    self > other
    """
    def __gt__(self, other):
        # 比较对象1>对象2的时候是比较他们的height属性
        return self.height > other.height

    # 重载:<
    def __lt__(self, other):
        return self.age < other.age

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



if __name__ == '__main__':
    stu = Student()
    stu.run('King')

    stu1 = Student2('aa', 18, 170)
    stu2 = Student2('bb', 10, 140)

    if stu1 > stu2:
        print('==========')

输出结果:
King ran
==========

04-python内存管理

上一篇 下一篇

猜你喜欢

热点阅读