Python中的类属性、实例属性与类方法、静态方法

1、什么是类对象,实例对象

类对象:类名
实例对象:类创建的对象

2、类属性就是类对象所拥有的属性,它被所有类对象的实例对象所共有,在内存中只存在一个副本,这个和C++、Java中类的静态成员变量有点类似。对于公有的类属性,在类外可以通过类对象和实例对象访问

类属性


# 类属性
class people:
    name="Tom"    #公有的类属性
    __age=18      #私有的类属性
 
p=people()
print(p.name)   #实例对象
print(people.name)  #类对象
 
# print(p.__age)    #错误 不能在类外通过实例对象访问私有的类属性
print(people.__age) #错误 不能在类外同过类对象访问私有的类属性

实例属性
class people:
    name="tom"
 
p=people()
p.age=18
 
print(p.name)
print(p.age)    #实例属性是实例对象特有的,类对象不能拥有
 
print(people.name)
#print(people.age)  #错误:实例属性,不能通过类对象调用
也可以将实例属性放在构造方法中
 class people:
    name="tom"
    def  __init__(self,age):
        self.age=age
 
p=people(18)
 
print(p.name)
print(p.age)    #实例属性是实例对象特有的,类对象不能拥有
 
print(people.name)
# print(people.age)  #错误:实例属性,不能通过类对象调用
 

类属性和实例属性混合
class people:
    name="tom"      #类属性:实例对象和类对象可以同时调用
    def  __init__(self,age):    #实例属性
        self.age=age
 
p=people(18)    #实例对象
p.sex="男"       #实例属性
 
print(p.name)
print(p.age)    #实例属性是实例对象特有的,类对象不能拥有
print(p.sex)
 
print(people.name)  #类对象
# print(people.age)  #错误:实例属性,不能通过类对象调用
# print(people.sex)  #错误:实例属性,不能通过类对象调用

# 如果在类外修改类属性,必须通过类对象去引用然后进行修改。如果通过实例对象去引用,
# 会产生一个同名的实例属性,这种方式修改的是实例属性,不会影响到类属性,并且如果通过
# 实例对象引用该名称的属性,实例属性会强制屏蔽掉类属性,即引用的是实例属性,除非删除了该实例属性
class Animal:
    name="Panda"
 
print(Animal.name)  #类对象引用类属性
p=Animal()
print(p.name)       #实例对象引用类属性时,会产生一个同名的实例属性
p.name="dog"        #修改的只是实例属性的,不会影响到类属性
print(p.name)         #dog
print(Animal.name)    #panda
# 删除实例属性
del p.name
print(p.name)


class Test:
 
    num=100 #类属性
    def __init__(self):
        self.age=18     #实例属性
 
test1=Test()
test2=Test()
# test1.age=19
# test2.age=20
print(test1.num)    #100
test1.num=200       #如果通过对象去修改类属性,那么Python会自动给这个对象和这个类属性相同名字的实例属性
print(test1.num)    #200,只是修改了副本
print(Test.num)     #100
 
del test1.num        #删除之后,仍能打印
print(test1.num)

3、类方法
是类对象所拥有的方法,需要用修饰器@classmethod来标识其为类方法,对于类方法,第一个参数必须是类对象,一般以cls作为第一个参数(当然可以用其他名称的变量作为其第一个参数,但是大部分人都习惯以'cls'作为第一个参数的名字,就最好用'cls'了),能够通过实例对象和类对象去访问。

class people:
    country="china"
 
    @classmethod
    def getCountry(cls):
        return cls.country
p=people()
print(p.getCountry())   #实例对象调用类方法
print(people.getCountry())  #类对象调用类方法


类方法还有一个用途就是可以对类属性进行修改:
class people:
    country="china"
 
    @classmethod
    def getCountry(cls):
        return cls.country
    @classmethod
    def setCountry(cls,country):
        cls.country=country
 
 
p=people()
print(p.getCountry())   #实例对象调用类方法
print(people.getCountry())  #类对象调用类方法
 
p.setCountry("Japan")
 
print(p.getCountry())
print(people.getCountry())

4、静态方法
需要通过修饰器@staticmethod来进行修饰,静态方法不需要多定义参数


class people3:
    country="china"
 
    @staticmethod
    def getCountry():
        return people3.country
p=people3()
print(p.getCountry())   #实例对象调用类方法
print(people3.getCountry())  #类对象调用类方法

从类方法和实例方法以及静态方法的定义形式就可以看出来,类方法的第一个参数是类对象cls,那么通过cls引用的必定是类对象的属性和方法;而实例方法的第一个参数是实例对象self,那么通过self引用的可能是类属性、也有可能是实例属性(这个需要具体分析),不过在存在相同名称的类属性和实例属性的情况下,实例属性优先级更高。
静态方法中不需要额外定义参数,因此在静态方法中引用类属性的话,必须通过类对象来引用
--------------------- 
作者:龙骨 
来源:CSDN 
原文:https://blog.csdn.net/qq_31780525/article/details/72639491 
版权声明:本文为博主原创文章,转载请附上博文链接!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值