Python面向对象编程 - 浅析实例方法,静态方法,属性方法,类方法的应用和区别


简单阐述了如何获得私有属性,Python的继承逻辑以及原因,四种常用的方法。


获取私有属性

众所周知,Python中的类遍历当前方加了双下划线就无法从外部访问。
其实还是有方法访问的,因为Python并不会严格的让变量禁止被访问。

class person:
    def __init__(self,age=18,weight = 60):
        self.age = age
        self.__weight = weight

    def birthday(self):
        self.age+=1
        
    def getweight(self):
        return self.__weight
jack = person()
print(jack.age)
>>>18

print(jack.__weight)
>>>AttributeError: 'person' object has no attribute '__weight'

这里会报一个错,但是可以用另一种方法访问
print(print(jack._person__weight))
>>> 60
**** 

继承逻辑

Python继承使用的是C3算法,可以避免再菱形继承中碰到的一些问题,比如某个父类反而把子类覆盖了。
在这里插入图片描述在这里插入图片描述


类的常用方法

类内可以使用装饰器让方法变的更简单,还是使用刚才的例子

class person:
    def __init__(self,age=18,weight = 60):
        self.age = age
        self.__weight = weight

    def birthday(self):
        self.age+=1


实例方法

实例方法就是最普通的方法,必须把实例作为第一个参数输入进方法并调用

class person:
    def __init__(self,age=18,weight = 60):
        self.age = age
        self.__weight = weight

    def birthday(self):
        self.age+=1

jack = person()
print(jack.age)
>>>18
jack.birthday()
print(jack.age)
>>>19

静态方法

静态方法就是一种方法不需要把类或者对象当成参数传入进方法内。可以直接从类调用
静态方法的调用并不需要实例,可以直接从类上面调用,需要使用静态方法装饰器@staticmethod

class person:
    def __init__(self,age=18,weight = 60):
        self.age = age
        self.__weight = weight

    def birthday(self):
        self.age+=1

    @staticmethod
    def nextage(age):
        return age + 1


print(person.nextage(5))
>>> 6

属性方法

实际上编程中很多属性是不希望被用户直接修改的,我们可以通过一个方法来得到对应的属性。使用装饰器@property

class person:
    def __init__(self,age=18,weight = 60):
        self.age = age
        self.__weight = weight

    def birthday(self):
        self.age+=1
        
    def getweight(self):
        return self.__weight

当这样写的时候用户想要得到属性必须调用getweight方法,

weight = jack.getweight()

这个时候我们就希望能像访问年龄一样直接访问weight

@property
def weight(self):
    return self.__weight
这样写之后我们就能直接访问weight

print(jack.weight)
>>> 60

类方法

类方法一般在工厂模式中被使用,用于创建新的实例。
类方法的第一个参数为cls,把类传入方法中并调用。必须要使用装饰器@classmethod

class person:
    def __init__(self,age=18,weight = 60):
        self.age = age
        self.__weight = weight
        
	#实例方法
    def birthday(self):
        self.age+=1

	#静态方法
    @staticmethod
    def nextage(age):
        return age + 1

	#属性方法
    @property
    def weight(self):
        return self.__weight

	#类方法
    @classmethod
    def getchild(cls):
        return cls(age=0,weight=0.05)

jack = person()
child = jack.getchild()

print(child.age)
>>> 0
print(child.weight)
>>> 0.05

其实没有类方法也可以使用普通的实例方法达到同样的效果,比如下面的代码,使用type函数得到自己的对象的class,然后再用这个class创建一个新的实例,非常精妙。但是明显不如上面的写法更加易读和高效。

def anotherchild(self):
    cls = type(self)
    return cls(age=1,weight=3)
child2 = jack.anotherchild()
print(child2.age)
>>>1
print(child2.weight)
>>>3
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值