面向对象相关内置函数

一、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) # 圆的周长
property

通过property后,可以直接用对象.方法名  调用了,不用加括号。相当于把类中方法看成属性了。

可以向访问数据属性一样去访问area,会触发一个函数的执行,动态计算出一个值

# 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

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

jim = Person('吉姆',1.72,72)
print(jim.bmi)  # 24.337479718766904   可以向访问数据属性一样去访问bmi,会触发一个函数的执行,动态计算出一个值
property计算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)  #泰哥sb
tiger.name = '春哥'
print(tiger.name) #春哥sb
View Code
class Goods:
    discount = 0.8
    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)  #4.0
View Code
属性 查看 修改 删除
class Person:
    def __init__(self,name):
        self.__name = name
        self.price = 20
    @property
    def name(self):
        return self.__name
    @name.deleter
    def name(self):
        del self.__name
    @name.setter
    def name(self,new_name):
        self.__name = new_name
brother2 = Person('二哥')
del Person.price
brother2.name = 'newName'
brother2
del brother2.name
print(brother2.name)
属性 查看 修改 删除

一个静态属性property本质就是实现了get,set,delete三种方法

二、classmethod 类方法

class Goods:
    __discount = 0.8
    def __init__(self,name,price):
        self.name = name
        self.__price = price
    @property
    def price(self):
        return self.__price * Goods.__discount
    @classmethod   # 把一个方法 变成一个类中的方法,这个方法就直接可以被类调用,不需要依托任何对象
    def change_discount(cls,new_discount):  # 修改折扣
        cls.__discount = new_discount
apple = Goods('苹果',5)
print(apple.price)
Goods.change_discount(0.5)   # Goods.change_discount(Goods)
print(apple.price)
# 当这个方法的操作只要涉及静态属性的时候 就应该使用classmethod来装饰这个方法
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
# 在完全面向对象的程序中,
# 如果一个函数 既和对象没有关系 也和类没有关系 那么就用staticmethod将这个函数变成一个静态方法

类方法和静态方法都是类调用的。

对象可以调用类方法和静态方法,但是,一般情况下,推荐用类名调用。

类方法 有一个默认参数 cls 代表这个类。

静态方法,没有默认的参数,就像函数一样

原文地址:https://www.cnblogs.com/huangjm263/p/8318558.html