• day26静态方法和反射


    #method方法
    #property方法 *****
    #staticmathod 静态方法 ***
    #classmethod 类方法 ****
    class Goods:
    __discount = 0.5
    def __init__(self,name,price):
    self.name = name
    self.__price = price
    @property
    def price(self):
    return self.__price*Goods.__discount
    @classmethod #把一个方法变成类中的方法,这个方法就直接可以被类调用,不需要依托任何对象
    def change_dicount(cls,new_dicount):
    cls.__discount = new_dicount

    apple = Goods('苹果',5)
    print(apple.price)
    Goods.change_dicount(0.5)
    print(apple.price)
    #当这个方法的操作只涉及静态属性的时候,就应该使用classmethod来装饰这个方法

    #staticmethod
    class Login:
    def __init__(self,name,password):
    self.name = name
    self.pwd = password
    def login(self):pass

    @staticmethod
    def get_usr_pwd(): # 静态方法
    usr = input('用户名 :')
    pwd = input('密码 :')
    Login(usr,pwd)
    Login.get_usr_pwd()

    #在完全面向对象的程序汇总,如果一个函数 既和对象没有关系,那么就用staticmethod将函数变成一个静态方法


    #类方法和静态方法 都是类调用的
    #对象可以调用类方法和静态方法吗? 可以 一般情况下 推荐用类名调用
    #类方法 有一个默认参数cls,代表这个类 cls
    #静态方法 没有默认参数 ,就像函数一样

    #property:把类中的方法伪装成一个属性
    #内置装饰器函数,只在面向对象中使用
    from math import pi
    class Circle:
    def __init__(self,r):
    self.r = r
    @property
    def perimeter(self):
    return 2*pi*self.r
    @property
    def area(self):
    return self.r**2*pi
    c1 = Circle(5)
    print(c1.area)
    print(c1.perimeter)

    class Person:
    def __init__(self,name,high,weight):
    self.name = name
    self.high = high
    self.weight = weight
    @property
    def bmi(self):
    return self.weight / self.high**2

    jin = Person('金老板',1.6,90)
    print(jin.bmi)

    class Person:
    def __init__(self,name):
    self.__name = name
    @property
    def name(self):
    return self.__name + 'sb'
    @name.setter
    def name(self,new_name):
    self.__name = new_name

    tiger = Person('泰哥')
    print(tiger.name)
    tiger.name = '全班'
    print(tiger.name)


    class Goods:
    discount = 0.5
    def __init__(self,name,price):
    self.name = name
    self.__price = price
    @property
    def price(self):
    return self.__price*Goods.discount
    apple = Goods('苹果',5)
    print(apple.price)

    #属性
    #查看 修改 删除
    class Person:
    def __init__(self,name):
    self.__name = name
    @property
    def name(self):
    return self.__name
    @name.deleter
    def name(self):
    print("执行了这个方法")
    del self.__name
    @name.setter
    def name(self,new_name):
    self.__name = new_name
    brother2 = Person('erge')
    print(brother2.name)
    del brother2.name
    print(brother2.name)
    #classmethod
    #staticmethod


    #反射 *****
    class Teacher:
    dic = {'查看学生信息':'show_student','查看讲师信息':'show_teacher'}
    def show_student(self):
    print('show_student')
    def show_teacher(self):
    print('show_teacher')
    @classmethod
    def func(cls):
    print('hahaha')
    @classmethod
    def func(cls):
    print("hahahha")
    alex = Teacher()
    for k in Teacher.dic:
    print(k)
    key = input("输入需求:")
    print(Teacher.dic[key])
    #hascttr getattr delattr
    # if hasattr(Teacher,'func'):
    # ret = getattr(Teacher,'dic') # Teacher.dic # 类也是对象
    # print(ret)
    if hasattr(alex,Teacher.dic[key]):
    func = getattr(alex,Teacher.dic[key])
    func()
    # 通过反射
    # 对象名 获取对象属性 和 普通方法
    # 类名 获取静态属性 和类方法 和 静态方法

    # 普通方法 self
    # 静态方法 @staticmethod
    # 类方法 @classmethod
    # 属性方法 @property

    # 继承
    # 封装的
    class Room:
    def __init__(self,name,length,width):
    self.__name = name
    self.__length = length
    self.__width = width
    def area(self):
    return self.__length*self.__width
    def get_name(self):
    return self.__name
    def set_name(self,newName):
    if type(newName) is str and newName.isdigit() == False:
    self.__name = newName
    else:
    print("不合法的姓名")
    jin = Room('jinlaoban',2,1)
    print(jin.area())
    jin.set_name('2')
    print(jin.get_name())

    #假设父类的私有属性 不能被子类调用
    # #会用到私有的这个概念的场景
    #1.隐藏起一个属性,不想让类的外部调用
    #2.我想保护这个属性,不想让属性随意被改变
    #3.我想保护这个属性,不被子类继承
    class Foo:
    __key = '123'
    class Son(Foo):
    print(Foo.__key)
  • 相关阅读:
    Coursera Machine Learning : Regression 简单回归
    KVM 虚拟化 初体验
    Spark RDD aggregateByKey
    Spark standalone HA
    Coursera 机器学习课程 机器学习基础:案例研究 证书
    dos学习笔记
    【???】 ???? 题解
    【ural 1223】 Chernobyl' Eagle on a Roof 题解
    由《鹰蛋》一题引发的思考
    【CF1408A】 Circle Coloring 题解
  • 原文地址:https://www.cnblogs.com/Murraya/p/11209916.html
Copyright © 2020-2023  润新知