python寒假培训第二课

super()函数

Python3中可以使用super()替代super().init()

作用:在子类中调用父类的方法进行使用

a.单继承(只继承一个父类,父类没有继承其他类)

class A(object):
    def __init__(self):
        print('i am class A')
        
class C(A):
    def __init__(self):
        super(C, self).__init__()  
# super(C self).init() 这句话的解释是:
#C是类,self是C的一个实例,super把self转化为父类A的一个实例对象,然后去使用init()方法
        print('yes')

c = C()

b.多重继承(父类也是继承了对象的类的)

class A(object):
    def __init__(self):
        print('i am class A')

class B(A):
    def __init__(self):
        super(B, self).__init__()
        print('i am class B')

class C(B):
    def __init__(self):
        super(C, self).__init__()
        print('yes')

c = C()

# 这里每一级super函数只是调用上一级的父类函数而已,所以C调用的是B的init函数,然后如果B的类里面也# 调用了A的init函数,就会先执行,

c.多继承(继承多个单独的类,不是多级的关系)

class A(object):
    def __init__(self):
        print('i am class A')

    def foo(self):
        print('i am another A')

class B():
    def __init__(self):
        print('i am class B')
    def foo(self):
        print('i am another B')

class C(A,B):
    def __init__(self):
        super(C, self).__init__()
        print('yes')

c = C()
c.foo()

#结果是继承了A的属性,然后调用时先调用了A的函数,然后使用实例调用foo函数,打印出来的也是A的foo
#如果我们写成class C(B,A)则会先调用B的属性


 参考的连接:(44条消息) Python3中的super函数_技术最差的的博客-CSDN博客_python3 super

(44条消息) Python中的super()用法_半遮雨的博客-CSDN博客_python super

property() 函数

作用:在新式类中返回属性值。

语法:  class property([fget[, fset[, fdel[, doc]]]])

  • fget -- 获取属性值的函数
  • fset -- 设置属性值的函数
  • fdel -- 删除属性值函数
  • doc -- 属性描述信息

使用方式:

a. 装饰器:在方法上应用装饰器

b. 类属性: 在类中定义值为 property 对象的类属性

可参考的教程 Python property() 函数 | 菜鸟教程 (runoob.com)

Python面向对象提升与收发邮件_哔哩哔哩_bilibili p5

装饰器 使用例子 :

该类的属性有三种访问方式,并分别对应三个被 @property、@方法名.setter、@方法名.deleter 修饰的方法,这三个方法对同一个属性:获取,修改,删除

class Money:
    def __init__(self):
        self._money = 0

    @property
    def money(self):
        return self._money

    @money.setter
    def money(self, value):
        self._money = value

    @money.deleter
    def money(self):
        print('我没钱了')
        del self._money


dollar = Money()
m = dollar.money  # 获取,dollar.money 在调用时没有()
print(m)
dollar.money = 10000000  # 修改
m = dollar.money
print(m)
del dollar.money  # 删除

类属性方式使用例子:

如果 c 是类 C 的实例化, c.x 将触发 getter,c.x = value 将触发 setter , del c.x 触发 deleter。

class Money:
    def __init__(self):
        self._money = 0

    def get_money(self):
        return self._money

    def setter_money(self, value):
        self._money = value

    def deleter_money(self):
        print('我没钱了')
        del self._money

    money = property(get_money, setter_money, deleter_money)


dollar = Money()
m = dollar.money
print(m)
dollar.money = 100000000
m = dollar.money
print(m)
del dollar.money

动态添加方法和属性

动态添加属性: 对象名.属性名

动态添加实例方法:引入 from types import MethodType,

MethodType第一个参数是函数本身,第二个是调用函数时,传给方法的第一个参数

更详细的讲解参考链接(44条消息) 【Python】 如何动态添加类属性与方法_本末实验室-CSDN博客_python添加类属性

一个B站博主举的例子:

from  types import MethodType

# 创建一个空类(即没有属性和方法)
class Person(object):
    __slots__ = ("name","age","speak")


per = Person()
# 动态添加属性,这体现了动态语言的特点灵活性
per.name = "aa"
print(per.name)


# 动态添加方法
def say(self):
 print("My name is" +self.name)
per.speak=MethodType(say, per) #调用say方法时默认per传入
per.speak()


#如果想要限制实例的属性,例如只允许给对象添加 name age 属性
# 解决:需要在定义类时,定义一个特殊的属性(__solt__)来限制动态添加的属性

培训作业1:动态给类添加方法,对猫的类中没有设置奔跑的方法,在类外添加这个方法,并打印

# 引入MethodType以便动态给类添加方法
from types import MethodType


# 创建一个猫类(设为空类,即没有属性和方法)
class Cat(object):
    def __init__(self, name, way):
        self.name = name
        self.way = way


# 实例化对象
c = Cat('大橘', '快速奔跑')

# 动态添加年龄属性,使用对象名.属性名添加,这体现了动态语言的特点灵活性
c.age = 1
# 输出动态添加的年龄属性
print(c.age)


# 动态添加run方法,打印输出猫的名称和奔跑方式
def run(self):
    print('猫的名字是%s,奔跑方式是%s' % (self.name, self.way))


# 实例化对象
p1 = Cat('大橘', '快速奔跑')
# MethodType第一个参数是函数本身,第二个是调用run函数时,传给run方法的第一个参数。
p1.run = MethodType(run, p1)
# 调用方法
p1.run()

运算符重载

定义:让自定义的类生成的对象(实例)能够使用运算符进行操作

好处:使程序简洁易读并对自定义对象将运算符赋予新的规则

运算符重载列表

一个简单的例子(B站):

class Person(object):
    def __init__(self, age):
        self.age = age

    # 运算符重载,self代表前一个对象,other代表后一个对象
    def __add__(self, other):   
        return self.age + other.age

    def __str__(self):
        return str(self.age)  # 数据类型转换


#实例化对象
p1 = Person(1)
p2 = Person(2)
print(p1 + p2)  # 等价于print( p1.__add__(p2))

培训作业2: 使用__pow__和__lt__两个运算符重载,对两个成绩进行幂运算和比较大小运算 

class Student(object):  # 定义一个学生类
    def __init__(self, grade):  # 定义构造方法
        self.grade = grade

    def __pow__(self, other):  # __pow__运算符重载,幂运算
        return self.grade ** other.grade

    def __lt__(self, other):  # __lt__运算符重载,判断前一个对象值是否小于后者,若是返回true否则返回false
        first = self.grade
        two = other.grade
        return first < two

    def __str__(self):
        return str(self.grade)  # 数据类型转换


p1 = Student(3)  # 第一个实例对象
p2 = Student(2)  # 第二个实例对象
print(p1 ** p2)  # 等价于print( p1.__pow__(p2))
print(p1 < p2)  # 等价于print( p1.__lt__(p2))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值