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

来源:互联网 发布:远程语音软件 编辑:程序博客网 时间:2024/06/05 17:44

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=18print(p.name)print(p.age)    #实例属性是实例对象特有的,类对象不能拥有print(people.name)#print(people.age)  #错误:实例属性,不能通过类对象调用

也可以将实例属性放在构造方法中

class people:    name="tom"    def  __init__(self,age):        self.age=agep=people(18)print(p.name)print(p.age)    #实例属性是实例对象特有的,类对象不能拥有print(people.name)# print(people.age)  #错误:实例属性,不能通过类对象调用

类属性和实例属性混合

class people:    name="tom"      #类属性:实例对象和类对象可以同时调用    def  __init__(self,age):    #实例属性        self.age=agep=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)         #dogprint(Animal.name)    #panda# 删除实例属性del p.nameprint(p.name)


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


3、类方法

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

class people:    country="china"    @classmethod    def getCountry(cls):        return cls.countryp=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=countryp=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.countryp=people3()print(p.getCountry())   #实例对象调用类方法print(people3.getCountry())  #类对象调用类方法


从类方法和实例方法以及静态方法的定义形式就可以看出来,类方法的第一个参数是类对象cls,那么通过cls引用的必定是类对象的属性和方法;而实例方法的第一个参数是实例对象self,那么通过self引用的可能是类属性、也有可能是实例属性(这个需要具体分析),不过在存在相同名称的类属性和实例属性的情况下,实例属性优先级更高。

静态方法中不需要额外定义参数,因此在静态方法中引用类属性的话,必须通过类对象来引用

阅读全文
0 0
原创粉丝点击