面向对象 2
静态方法和类方法
1.静态方法
- 通过装饰器
@staticmethod
来进行装饰。静态方法既不需要传递类对象也不需要传递实例对象 - 静态方法也可以通过实例对象和类对象访问
class Dog:
type = "狗"
def __init__(self):
name = None
# 静态方法
@staticmethod
def introduce(): # 静态方法不会自动传递实例对象和类对象
print("犬科哺乳动物,属于肉食目")
dog = Dog()
Dog.introduce()
dog.introduce()
静态方法是类中的函数,不需要实例。
静态方法主要是用来存放逻辑性的代码,逻辑上属于类,但和类本身没有关系,也就是说,在静态方法中,不会涉及类中属性和方法的操作。
—>静态方法是一个独立的,单纯的函数,仅仅是托管与某个类的名称空间中,便于维护和管理。
使用场景:
- 当方法中 既不需要使用实例对象(如实例对象,实例属性),也不需要使用类对象 (如类属性、类方法、创建实例等)时,定义静态方法
- 取消不需要的参数传递,有利于 减少不必要的内存占用和性能消耗
- 如果在类外面写一个同样的函数来做这些事,打乱了逻辑关系,导致代码维护困难,使用静态方法。
2.类方法
- 类对象所拥有的方法
- 需要用装饰器
@classmethod
来标识其为类方法。 - 对于类方法,第一个参数必须时类对象,一般以cls作为第一个参数
class Dog:
__type = "狗"
# 类方法,用class来进行装饰
@classmethod
def get_type(cls):
return cls.__type
print(Dog.get_type())
使用场景:
- 当方法中需要使用类对象(如访问私有类属性等)时,定义类方法。
- 类方法一般和类属性配合使用。
注意:
类中定义了同名的对象方法,类方法以及静态方法时,调用方法会优先执行最后定义的方法。
class Dog:
def demo_method(self):
print("对象方法")
@classmethod
def demo_method(cls):
print("类方法")
@staticmethod
def demo_method():
print("静态方法")
dog = Dog()
Dog.demo_method()
dog.demo_method()
# 静态方法
# 静态方法
property
概述
在Python中主要为属性提供一个便利的操作方式。
如果我们现在需要设计一个银行账户类,这个类包括账户人的姓名,余额。(加入不考虑具体操作接口)
简单实现:
class Account:
def __init__(self,name,money):
self.name = name
self.money = money
问题:
不安全(设计简单方便,所有的属性,外部可以访问修改,非常不安全)
改进 隐藏实现细节
对于账户信息而言,金额不允许让用户直接修改。如果修改,只能去窗口办理。
程序 的实现:
—>在使用对象时,尽量不要让使用者直接操作对象中的属性,因为直接操作会带来安全隐患。
这个时候,考虑私有属性。
class Account:
def __init__(self,name,money):
self.__name = name
self.__money = money
代码改进后,将所有属性设计成私有属性后。确实从外部使用时,不能直接修改对象,隐藏了实现的细节。
改进 实现数据操作
但是如果确实需要进行修改属性需要添加方法
class Account:
def __init__(self,name,money):
self.__name = name
self.__balance = money
def get_name(self):
return self.__name
def set_balance(self,money):
self.__balance = money
def get_balance(self):
return self.__balance
经过修改,外部使用这个类的对象时,想使用对象中的属性,只能通过类中提供的 set/get 接口来操作,提高了程序的安全性。
这样,程序基本达到了设计需求,但是能不能更加完善呢?
如果在使用这个类的对象过程中,由于错误操作,传入了不正常的数据,导致数据异常。该如何以避免这种情况发生呢?
比如:设置金额时出现了负数,或字符串,或其它类型的对象。
改进 保证数据的有效性
set_balance方法中,对传入数据进行有效判断,如果传入数据错误,触发数据异常错误。
class Account:
def __init__(self,name,money):
self.__name = name
self.__balance = money
def get_name(self):
return self.__name
def set_balance(self,money):
if isinstance(money,int):
if money > 0 :
self.__balance = money
else:
raise ValueError('输入金额不正确')
else:
raise ValueError('输入金额不是数据')
def get_balance(self):
return self.__balance
经过几代版本的迭代,程序看上去越来越健壮,安全性也越来越高。
但是使用过程中,可不可以更精炼一些?
属性操作
property 类
在Python中,提供了一个property类,通过对创建这个类的对象的设置,在使用对象的私有属性时,可以不再使用属性的函数的调用方式,而是像普通的公有属性一样去去使用属性,为开发者提供便利。
property(fget=None,fset=None,fdel=None,doc=None) # property attribute
property 是一个类,__init__方法由四个参数构成,实例后返回一个用来操作属性的对象
- 参数一:属性的获取方法
- 参数二:属性的设置方法
- 参数三:属性的删除方法
- 属性四:属性描述
class Account:
def __init__(self,name,money):
self.__name = name
self.__balance = money
def __get_name(self):
return self.__name
def set_balance(self,money):
if isinstance(money,int):
if money > 0 :
self.__balance = money
else:
raise ValueError('输入金额不正确')
else:
raise ValueError('输入金额不是数据')
def get_balance(self):
return self.__balance
# 使用property 类来为属性设置便利的访问方式
name = property(__get_name)
balance = property(get_balance,set_balance)
ac = Account("Tom",10000)
print(ac.name)
print(ac.balance)
ac.set_balance(1000000)
print(ac.balance)
# Tom
# 10000
# 1000000
通过 property 类实例对象以后,在使用对象中的属性时,就可以像使用普通公有属性一样来调用,但是实际调用的还是 set/get 方法。 在实例 property 对象时,不是所有的参数都需要写,比如示例中的 name 只提供了 get 方法,并且是一个私有的方法。这样就完全隐藏了内部的实现细节 。
class Account:
def __init__(self,name,money):
self.__name = name
self.__balance = money
@property
def name(self):
return self.__name
@property
def balance(self):
return self.__balance
@balance.setter
def balance(self,money):
if isinstance(money,int):
if money > 0 :
self.__balance = money
else:
raise ValueError('输入金额不正确')
else:
raise ValueError('输入金额不是数据')
# 使用property 类来为属性设置便利的访问方式
# name = property(__get_name)
# balance = property(get_balance,set_balance)
ac = Account("Tom",10000)
print(ac.name)
print(ac.balance)
ac.balance = 100000000
print(ac.balance)
# Tom
# 10000
# 1000000
self
如果对象的方法中需要使用该对象的属性,该怎么办?
- 关键字
self
主要用于对象方法中,表示调用该方法的对象。 - 在方法中使用self,可以获取到调用当前方法的对象,进而获取该对象的属性和方法。
调用对象的方法时,为什么不需要设置self对应的参数?
- 某个对象调用其方法时,Python解释器会把这个对象作为第一个参数传递给方法,所以,开发者只需要在定义的时候“预留第一个参数self即可。
class Cat:
# 方法
def introduce(self):
print("name is %s,age is %d"%(self.name,self.age))
# 实例化,创建一个对象
cat = Cat()
cat.name = "蓝猫"
cat.age = 6
cat.introduce()
# name is 蓝猫,age is 6
方法内定义属性
- 使用self操作属性和对象的变量名,在效果上类似。如果属性在赋值时还没有被定义,就会自动定义一个属性并赋值。
class Cat:
def introduce(self):
self.type = "小型动物"
cat = Cat()
cat.introduce()
print(cat.type)
__new__
方法
-
创建对象时,系统会自动调整
__new__
方法。 -
开发者可以使用
__new__
方法来自定义对象的创建过程。 -
__new__
至少要有一个参数cls,代表要实例化的类,此参数在实例化时由Python解释器自动提供 -
__new__
必须要有返回值,返回实例化出来的实例,这点在自己实现__new__
时要特别注意,可以return父类__new__
出来的实例,或者直接是object的__new__
出来的实例 -
__init__
有一个参数self,就是这个__new__
返回的实例,__init__
在__new__
的基础上可以完成一些其它初始化的动作,__init__
不需要返回值 -
如果创建对象时传递了自定义参数,且重写了
__new__
方法,则new也必须 “预留” 该形参,否则__init__
方法将无法获取到该参数
class Cat:
def __new__(cls, name):
print("创建对象")
return object.__new__(cls)
def __init__(self,name):
print("对象初始化")
self.name = name
def __str__(self):
return "%s"% self.name
cat = Cat("蓝猫")
print(cat)
# 创建对象
# 对象初始化
# 蓝猫
class A(object):
def __new__(cls, x):
print('this is in A.__new__, and x is ', x)
return super(A, cls).__new__(cls)
def __init__(self, y):
print('this is in A.__init__, and y is ', y)
class C(object):
def __new__(cls, n):
print('this is in C.__new__, and n is ', n)
return super(C, cls).__new__(cls)
def __init__(self, a):
print( 'this is in C.__init__, and a is ', a)
class B(A):
def __new__(cls, z):
print('this is in B.__new__, and z is ', z)
return A.__new__(cls, z)
def __init__(self, m):
print('this is in B.__init__, and m is ', m)
# class B(A):
# def __new__(cls, z):
# print 'this is in B.__new__, and z is ', z
# return object.__new__(cls) # 将不返回A.__new__(cls)
# def __init__(self, m):
# print 'this is ni B.__init__, and m is ', m
if __name__ == '__main__':
a = A(100)
print('=' * 20)
b = B(200)
print( type(b))
# this is in A.__new__, and x is 100
# # this is in A.__init__, and y is 100
# # ====================
# # this is in B.__new__, and z is 200
# # this is in A.__new__, and x is 200 # 将不返回A.__new__(cls)
# # this is in B.__init__, and m is 200
# # <class '__main__.B'>
__call__
方法
对象后面加括号,触发执行
构造方法的执行是有创建对象触发的,即:对象 = 类名()
而对于,__call__
方法的执行是由对象后加括号触发的,即对象()或者类()
class A(object):
def __init__(self):
pass
def __call__(self, x):
print('__call__ called, print x: ', x)
a = A()
a('123')
# __call__ called, print x: 123
__doc__
无法继承给子类
class Foo:
'''这是一段说明'''
pass
class Bar(Foo):
def __call__(self):
return Foo.__doc__
print(Foo.__doc__)
print(Bar.__doc__) #
# 这是一段说明
# None