Python之类的成员

一、私有变量和私有方法

私有属性分为两类分别是对象的私有变量以及类的静态私有变量,除了变量外,还有私有方法;在类中,所有私有的属性和方法都是在变量的左边加上双下划线的方式定义,所有的私有变量和私有方法都不能在类的外部使用

class Person():
    __pwd = "123456"  # 私有静态变量

    def __init__(self,name,price):
        self.name = name
        self.__price = price   # 私有变量

    # 私有方法
    def __add_price(self):
        return self.__price*2

eric= Person("eric",20000000)
# 私有变量和方法无法在外部调用,虽然可以通过_类名__属性名方式调用私有变量,但不提倡使用
print(eric._Person__price)
print(eric._Person__pwd)
print(eric._Person__add_price())
20000000
123456
40000000

二、类的属性

python的属性其实是普通方法的变种
有两种定义方式:
1、普通方法加上property装饰器
2、在类中定义值为property对象的静态字段

property装饰器方式

property是一种特殊的属性,当我们访问被property装饰的函数时,执行函数内的代码,得到返回值。

from math import pi

class Circle():
    def __init__(self,radius):
        self.radius = radius

    @property
    def perimeter(self):
        return 2*pi*self.radius

    @property
    def area(self):
        return self.radius**2*pi

c1 = Circle(5)

# 未带装饰器调用方法
# print(c1.area())

# 带装饰器时当成属性调用
print(c1.area)

使用property装饰器定义属性时,要注意被property装饰的普通方法只能有self一个参数,并且在调用时不需要加()。

在python3之后,所有的类默认继承了object,所以说,所有的类都是新式类,新式类的property有三种访问方式,并分别对应了三个被@property、@方法名.setter、@方法名.deleter修饰的方法

class Goods(object):

    def __init__(self):
        # 原价
        self.original_price = 100
        # 折扣
        self.discount = 0.8

    @property
    def price(self):
        # 实际价格 = 原价 * 折扣
        new_price = self.original_price * self.discount
        return new_price

    @price.setter
    def price(self, value):
        self.original_price = value

    @price.deleter
    def price(self):
        del self.original_price

obj = Goods()
print(obj.price)         # 获取商品价格
obj.price = 200   # 修改商品原价
del obj.price     # 删除商品原价

静态字段方式

静态字段方式没有新式类和经典类的区别;静态字段定义方式也有三种访问方式:分别对应了三个被@property、@方法名.setter、@方法名.deleter修饰的方法

class Person():
    def __init__(self,name):
        self.__name = name

    def get_name(self):
        return self.__name

    def set_name(self,newname):
        self.__name = newname

    def del_name(self):
        del self.__name

    NAME = property(get_name,set_name,del_name)   #定义property静态字段

obj = Person("eric")

print(obj.NAME)     #调用get_name方法
obj.NAME = "111"    #调用set_name方法
del Person.NAME     #调用del_name方法

三、类的方法

方法包括:普通方法、静态方法和类方法,三种方法在内存中都归属于类,区别在于调用方式不同。

普通方法:由对象调用;至少一个self参数;执行普通方法时,自动将调用该方法的对象赋值给self;
类方法:由类调用; 至少一个cls参数;执行类方法时,自动将调用该方法的类复制给cls;
静态方法:由类调用;无默认参数;
class Goods():
    __discount = 0.8
    def __init__(self,name,price):
        self.name = name
        self.__price = price

    def price(self):   # 普通方法
        return self.__price*Goods.__discount

    @classmethod
    def change_discount(cls,new_discount):  # 类方法
        cls.__discount = new_discount

    @staticmethod
    def get_user_pwd():     #静态方法
        user = input('please input username: ')
        pwd = input('please input pwd: ')

apple = Goods('apple',10)
Goods.change_discount(0.9)   # 类方法调用
Goods.get_user_pwd()         # 静态方法调用

小结:
1、类方法
类方法是把一个方法,变成一个类中的方法,这个方法就直接可以被类调用,不需要通过对象来调用;
类方法一般用于只涉及操作静态变量时使用。
2、静态方法
一个函数,在与对象以及类没什么关系的情况下,要完全的面向对象编程,此时,就可以用staticmethod将这个函数变成类中的静态方法。

猜你喜欢

转载自blog.51cto.com/jiayimeng/2343402