python中的类的继承、多态和运算符重载

2019-07-18  本文已影响0人  发家致富靠养猪

类的继承

1.继承

a.什么是继承
b.可以继承哪些内容
class Person(object):
    num = 61

    # 注意:__slots__对应的值不会被继承
    __slots__ = ('name', 'age', 'sex')

    def __init__(self):
        self.name = '张三'
        self.age = 0
        self.sex = '男'

    def show_message(self):
        print('%s你好吗?' % self.name)


# Student类继承自Person类
class Student(Person):
    pass


# 创建学生对象
stu1 = Student()
# 对象属性可以继承
print(stu1.name, stu1.age, stu1.sex)

# 类的字段可以继承
print(Student.num)

# 对象方法可以继承
stu1.show_message()



p1 = Person()
# p1.color = '黄色'
stu1.color = '白色'
print(stu1.color)

子类-添加方法

1.在子类中添加方法

a.添加一个新的方法
b.重写父类的方法: 重新实现父类的方法

完全重写 - 覆盖父类的功能 - 直接在子类中重新实现父类的方法
部分重写 - 保留父类的功能,添加新的功能 - 在子类中实现父类方法的时候通过super()去调用父类的方法,
再添加新的功能
注意:a.可以子类的方法中通过super()去调用父类的方法
super(类, 对象)- 获取对象中父类的部分(要求对象是这个指定的类的对象)
b.静态方法中不能使用super()

c.类中方法的调用过程

通过对象或者类调用方法的时候,先看当前类中是否声明过这个方法,如果声明过就直接调用当前类对应的方法;
如果当前类中没有声明过,会去找父类中有没有声明过这个方法,声明过就调用父类的方法;
如果父类中也没有声明过,就去找父类的父类...以此类推,直到object中也没有声明过,程序才会崩溃


class Person:
    # 类的字段
    num = 61

    # 对象属性
    def __init__(self):
        self.name = '张三'
        self.age = 0
        self.sex = '男'

    def fun1(self):
        print('Person的对象方法')

    # 方法
    def show_message(self):
        print('%s,你好吗?' % self.name)

    @staticmethod
    def info():
        print('我是人类')


class Student(Person):

    def study(self):
        print('%s在学生' % self.name)

    @classmethod
    def message(cls):
        super().info()
        print('我是学生!')

    # 完全重写
    @staticmethod
    def info():
        print('我是学生!!!')

    # 保留父类的功能
    def show_message(self):
        super().show_message()
        print('我去上学~')
        super().fun1()

# Student.info()

子类-添加属性

1.添加类的字段

2.添加对象属性

class Person:
    num = 61

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


class Student(Person):
    number = 100

    def __init__(self, name):
        super().__init__(name)
        self.study_id = '001'


print(Student.number, Student.num)

stu1 = Student('小明')
print(stu1.name, stu1.age, stu1.study_id)

# 练习:
# 声明一个动物类,有属性:年龄,颜色,类型。
#  要求创建动物对象的时候类型和颜色必须赋值,年龄可以赋值也可以不赋值
# 声明一个猫类,有属性:年龄,颜色,类型, 爱好
# 要求创建猫对象的时候,颜色必须赋值,年龄和爱好可以赋值也可以不赋值,类型不能赋值


class Aniaml:
    def __init__(self, type, color, age=0):
        self.type = type
        self.color = color
        self.age = age


class Cat(Aniaml):
    def __init__(self, color, age=0, hobby=''):
        super().__init__('猫科', color, age)
        self.hobby = hobby


an1 = Aniaml('犬科', '黄色')
an2 = Aniaml('犬科', '黄色', 10)

cat1 = Cat('白色')
cat2 = Cat('灰色', 3)
cat3 = Cat('灰色', hobby='睡觉')
cat4 = Cat('灰色', 3, '睡觉')


多继承

class Animal:
    num=61
    def __init__(self,name='小红',age=0,color='黑色'):
        self.name = name
        self.age = age
        self.color = color
    def show_info(self):
        print('名字:%s 年龄:%s 颜色:%s'%(self.name,self.age,self.color))



class Fly:
    info = '飞'
    def __init__(self,distance=0,speed=0):
        self.distance = distance
        self.speed = speed
    @staticmethod
    def show():
        print('飞')
#让birds同时继承Animal,Fly
class Birds(Animal,Fly):
    pass
#两个类的字段都会继承
print(Birds.num,Birds.info)
Birds.show()

#两个类的方法都能继承,对象属性只能继承第一个类的对象属性
b1 = Birds()
print(b1.name)

多态

封装:可以对多条数据(属性)和多个功能(方法)进行封装
继承:可以让一个类拥有另一个类的属性和方法
多态:有继承就有多态(一个事物的多种形态)

运算符重载

1.python中函数不支持重载,

2.运算符重载

class Student(object):
    def __init__(self,name='',score=0,age=0):
        self.name=name
        self.score=score
        self.age = age

    #  +  重载
    '''
    数据1 + 数据2 -- 数据1或传给self,数据2会传给other
    '''
    def __add__(self, other):
        #self是+前面的,other是+后面的
        return self.score + other.score
    # - 运算
    def __sub__(self, other):
        #self - other
        return self.score - other.score

    #注意 大于和小雨一般情况下只需要重载一个,另外一个自动支持
    # < 运算
    def __lt__(self, other):
        return self.score < other.score
    #  > 运算
    def __gt__(self, other):
        return self.age > other.age

    def __repr__(self):
        return '>'+(str(self.__dict__)[1:-1])+'<'



stu1 = Student('小红',99,18)
stu2 = Student('小花',95,19)
stu3 = Student('小黄',96,18)
stu4 = Student('小蓝',97,19)
print(stu1 + stu2)
print(stu1 - stu2)
all_student = [stu1,stu2,stu3,stu4]
print(all_student)

print(max(all_student))

上一篇 下一篇

猜你喜欢

热点阅读