zoukankan      html  css  js  c++  java
  • py.面向对象,属性,类方法,静态方法,反射

    例一:BMI指数(bmi是计算而来的,
    # 但很明显它听起来像是一个属性而非方法,如果我们将其做成一个属性,更便于理解)
    #
    # 成人的BMI数值:
    # 过轻:低于18.5
    # 正常:18.5-23.9
    # 过重:24-27
    # 肥胖:28-32
    # 非常肥胖, 高于32
    #   体质指数(BMI)=体重(kg)÷身高^2(m)
    #   EX:70kg÷(1.75×1.75)=22.86

    # 1,初识属性:
    # class A:
    # def __init__(self, name, weight, height):
    # self.name = name
    # self.__weight = weight
    # self.__height = height
    #
    # @property # 属性
    # def bmi(self):
    # return self.__weight / self.__height ** 2
    #
    #
    #
    # ly = A('女司机', 54, 1.6)
    # print(ly.bmi)
    #
    # # 属性:将方法 伪装 成属性,虽然在代码层面上没有任何高深之处,
    # # 但是让其看起来更合理.
    # # 2,属性改,和删除.
    #
    # class Mes:
    # def __init__(self, name, age,password):
    # self.__name = name
    # self.__age = age
    # self.__password = password
    #
    # @property
    # def password(self):
    # '''对密码加密'''
    # self.__password = hash(self.__password + '旭哥')
    # return self.__password
    #
    # @password.setter
    # def password(self,new_password):
    # if type(new_password) is str:
    # self.__password = new_password
    # else:
    # print('请输入字符串类型')
    # @password.deleter
    # def password(self):
    # print(6666)
    # # p1 = Mes('婉容', 18)
    # # p1.name = '婉容蓉'
    # # print(p1.name)
    # # print(p1.name)
    # p1 = Mes('婉容', 18,'hao123')
    # # print(p1.password)
    # 之前对属性的更改
    # p1.name = 'alex'
    # p1.password = '123456'
    # print(p1.password)
    # del p1.password
    # print(p1.password)
    # 第一步,对属性进行改操作,自动执行 被这个@password.setter装饰器装饰的方法
    # 第二步,将新值传给这个被@password.setter装饰器装饰的方法里面,当做参数.

    # property ***
    # @password.setter *
    # @password.deleter
    #
    # class Mes:
    # def __init__(self, name, age,password):
    # self.name = name
    # self.__age = age
    # self.__password = password
    #
    # @property
    # def password(self):
    # return self.__password
    #
    # @password.setter
    # def password(self,new_password):
    # self.__password = new_password
    #
    # @password.deleter
    # def password(self):
    # del self.__password
    #
    # p1 = Mes('婉容', 18, 'hao123')
    # p1.name = 'alex'
    # print(p1.name)
    # print(p1.password)
    # p1.password = '123456' # 这是是行代码,没有真正的改.
    # print(p1.password)
    # del p1.password
    # print(p1.password)

    # 商品类 封装商品名,商品原价, 商品折扣 定义一个方法,计算商品现价.

    # class Goods:
    # def __init__(self,name,original_price,discount):
    # self.name = name
    # self.__original_price = original_price
    # self.__discount = discount
    #
    # @property
    # def price(self):
    # return self.__original_price * self.__discount
    #
    # @property
    # def original_price(self):
    # return self.__original_price
    #
    # @original_price.setter
    # def original_price(self,new_original_price):
    # self.__original_price = new_original_price
    #
    # apple = Goods('苹果',5,0.95)
    # # print(apple.price)
    # apple.original_price = 10
    # print(apple.price)


    # 类方法,静态方法

    class A:
    __name = 'alex'
    def func(self):
    print(self,'in func')

    @classmethod # 类方法
    def func1(cls):
    print(cls, 'in func1')

    def change(self,new_name):
    A.__name = new_name
    return A.__name
    @classmethod
    def change(cls,new_name):
    A.__name = new_name
    return A.__name

    a1 = A()
    a1.func()
    A.func(a1)

    # 类名去调用类方法,自动将类的空间传给类方法中的cls
    # 对象如果调用类方法,自动将类空间传给类中的cls
    A.func1()
    a1.func1()
    # 什么情况下使用类方法?
    # 直接让类去操作类中的方法,没有必要创建对象在操作的时候,用类方法.
    # a2 = A()
    # print(a2.change('wusir'))
    # # print(A.change(111,'wusir'))
    # print(A.change('wusir'))
    # 静态方法

    # class A:
    # __name = 'alex'
    # def func(self):
    # print(self,'in func')
    #
    # @classmethod # 类方法
    # def func1(cls):
    # print(cls, 'in func1')
    #
    #
    # @staticmethod # 静态方法
    # def login(username,password):
    # print('登录成功...')
    #
    # A.login('alex', '123')

    # 静态方法:在类中定义一个静态方法,无需传入你的类空间,对象空间,可以当成普通函数去用.


    反射"""""""
     反射
    # 非常非常非常非常重要的方法.
    # 通过 字符串 操作一个空间 (对象).
    # getattr()
    # hasattr()
    # setattr()
    # delattr()

    # 1,实例化一个对象去研究
    # class A:
    # country = 'China'
    #
    # def __init__(self, name, age):
    # self.name = name
    # self.age = age
    #
    #
    # a1 = A('alex', 1000)
    # print(a1.name)
    # print(getattr(a1,'name1')) 从对象中得到这个属性对应的值
    # print(hasattr(a1,'age')) 判断此对象中,有没有这个属性
    # setattr(a1,'sex','男')
    # setattr(a1,'name','wusir')
    # print(getattr(a1,'name'))
    # print(getattr(a1,'sex')) # 对一个对象设置属性
    # delattr(a1,'name') # 对一个对象属性删除
    # print(getattr(a1,'name'))

    # if hasattr(a1,'name1'):
    # getattr(a1,'name1')
    # else:
    # print('没有...')


    # 2,类中去研究.
    # class A:
    # country = 'China'
    # job = 'student'
    #
    # def __init__(self, name, age):
    # self.name = name
    # self.age = age
    # def func(self):
    # print('in func')
    # print(getattr(A,'country1',False))
    # if getattr(A,'country1',False):
    # content = input('>>>').strip() # country
    # print(A.content) #(A.'country')

    # name = '1 + 2'
    # name2 = 'name'
    # print(eval(name2))


    # print(A.country)
    # print(A.content)
    # print(eval('1+ 2'))

    # content = input('>>>').strip() # 'country'
    # 你拿到的是一个字符串类型,然后你又想对这个类进行操作
    # if hasattr(A,content):
    # print(getattr(A,content))

    # print(getattr(A,'job1','没有此值'))
    # print(getattr(A,'func'))
    # getattr(A,'func')(11)

    # 3,其他模块去研究.
    import oldboy
    print(oldboy.B.name_list)

    bobj = getattr(oldboy,'B')
    print(getattr(bobj,'name_list'))
    #
    print(getattr(oldboy.B,'name_list'))

    print(getattr(oldboy.B,'add')(3,4))
    print(getattr(oldboy,'login')('alex','123'))
    # def func():
    # pass
    # print(func())


    # 4,本模块(本文件)去研究.
    # import sys
    # def login():
    # print(55)
    #
    # def func3():
    # print(333)
    #
    # # content = input(">>>")
    # # print(content()) # 这样错的
    # print(sys.modules[__name__])
    # getattr(sys.modules[__name__],'login')()
    "
  • 相关阅读:
    Solution: Win 10 和 Ubuntu 16.04 LTS双系统, Win 10 不能从grub启动
    在Ubuntu上如何往fcitx里添加输入法
    LaTeX 笔记---Q&A
    Hong Kong Regional Online Preliminary 2016 C. Classrooms
    Codeforces 711E ZS and The Birthday Paradox
    poj 2342 anniversary party
    poj 1088 滑雪
    poj 2479 maximum sum
    poj 2481 cows
    poj 2352 stars
  • 原文地址:https://www.cnblogs.com/cz007/p/9265973.html
Copyright © 2011-2022 走看看