练习37(issubclass和isinstance、classmethod和staticmethod)

练习37


 # 圆形类
 # bmi

 # 学生类
# class Student:
#     def __init__(self,name):
#         self.__name = name
#     @property
#     def name(self):
#         return self.__name
#     @name.setter
#     def name(self,new):
#         if type(new) is str:
#             self.__name = new
#         else:
#             print('类型错误')
#
# zhuge = Student('诸葛')
# # print(zhuge.name)
# zhuge.name = 123 # 只有当被property装饰的方法
# print(zhuge.name)
# 又实现了一个同名的方法
# 且被setter装饰器装饰了
# 且 在 被装饰器的方法 赋值的时候 就触发被setter装饰器装饰的方法

# 用来保护一个变量 在修改的时候能够添加一些保护条件

# class Goods:
#     __discount = 0.8
#     def __init__(self,price):
#         self.__price = price
#         self.name = 'apple'
#     @property
#     def price(self):
#         return self.__price * Goods.__discount
#
#     @price.setter
#     def price(self,new):
#         self.__price = new
#
#     @price.deleter
#     def price(self):
#         del self.__price
#
# apple = Goods(10)
# print(apple.price)
# print(apple.__dict__)
# del apple.price
# apple.price = 8
# # print(apple.price)
#
# # print(apple.__dict__)
# # del apple.name
# print(apple.__dict__)

# 一个方法被伪装成属性之后
# 应该可以执行一个属性的增删改查操作
# 那么增加和修改 就对应着被setter装饰的方法:这个方法有一个必传的参数new,表示赋值的时候等号后面的值
# 删除一个属性 对应着 被delter装饰的方法,这个方法并不能真的删除这个属性,而是在代码中执行什么就有什么效果


# class A:
#     def __init__(self):
#         self.__f = open()
#     @property
#     def f(self):
#         return self.__f
#
#     @f.deleter
#     def f(self):
#         self.__f.close()
#         del self.__f


# print(type(123) is int)
# print(isinstance(123,int))

# class A:
#     pass
# class B(A):
#     pass
# a = A()
# b = B()
# print(type(a) is A)
# print(type(b) is A)

# print(isinstance(a,A))
# print(isinstance(b,A))
# print(isinstance(a,B))
# 检测对象与类之间的关系

# class A:pass
# class B(A):pass
# print(issubclass(A,B))
# print(issubclass(B,A))
# 检测类与类之间的关系

# @classmethod

# class Goods:
#     __discount = 0.8 # 静态属性
#     def __init__(self,price):
#         self.__price = price # 对象属性
#         self.name = 'apple'
#     @property
#     def price(self):
#         return self.__price * Goods.__discount
#
#     @classmethod
#     def change_discount(cls,new): # 类方法
#         cls.__discount = new
#
#
# Goods.change_discount(0.7)
# print(Goods.__dict__)

# 类方法的特点
    # 只是使用类中的资源:且这个资源可以直接用类名应用的使用,那么这个方法应该被改编为一个类方法
        # 静态属性
        # 普通方法
        # 类方法
        # property方法
# apple = Goods(10)
# banana = Goods(20)
# apple.change_discount(0.7)
# print(apple.price)
# print(banana.price)


# class Student:
#     @staticmethod
#     def login(user,pwd): # 静态方法
#         print('in LOGIN',user,pwd)
#
# Student().login('usr','pwd')


# 类:
    # 静态属性    类  所有的对象都拥有的属性
    # 类方法      类  如果这个方法这几道操作静态属性、类方法、静态方法                    cls表示类
    # 静态方法    类  普通方法,不使用类中的命名空间,也不适用对象的命名空间:一个普通的函数   没有默认参数
    # 方法       对象                                                            self表示对象
    # property  对象                                                            self表示对象
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值