python 面向对象-实例变量、类变量、实例方法、类方法、静态方法
需要搞清楚的是,
- 实例方法与类方法是什么?类变量和实例变量又是什么?
- 类方法中如何访问类变量、类方法要如何调用?当然
- 实例方法中如何访问类变量?能
- 实例方法访问实例变量,实例方法如何被调用?当然
- 类方法中能否访问实例变量?不能
- 对象调用类方法?能
- 静态方法是什么?装饰器:@staticmethod
- 静态方法中如何访问类变量和实例变脸、静态方法如何调用?静态方法可以访问类变量、不能访问实例变量,静态方法可以用类名访问,也可以用实例(对象)访问
1、实例方法与类方法
实例方法:def do_homework(self):
类方法:def plus_sum(cls):
类变量:
name = ""
age = 0
sum1 = 1
实例变量:
self.name
self.age
注意:决定一个方法是类方法还是实例方法的决定性因素是@classmethod装饰器,跟入参cls没关系。我们的self 可以用cls 代替,也可以用其他入参名代替。
#coding=utf-8 class Student(): name = "" age = 0 sum1 = 1 def __init__(self,name,age): #构造函数 self.name = name self.age = age def do_homework(self):#实例方法 print(self.name +"do homework") @classmethod def plus_sum(cls): pass
2、类方法如何访问类变量;类方法如何调用
#coding=utf-8 class Student(): name = "" age = 0 sum = 0 def __init__(self,name,age): #构造函数 self.name = name self.age = age def do_homework(self):#实例方法 print(self.name +"do homework") @classmethod def plus_sum(cls):#类方法 cls.sum+=1#类方法访问类变量 print(cls.sum) student1 = Student("a",18) Student.plus_sum()#类方法调用,用类名调用类方法 student2 = Student("b",19) Student.plus_sum() student3 = Student("c",20) Student.plus_sum() # [Running] python -u "/Users/anson/Documents/Project/python_ToolCodes/test6.py" # 1 # 2 # 3
3、实例方法访问类变量
- 直接访问sum1,报错
#coding=utf-8 class Student(): name = "" age = 0 sum1 = 0 def __init__(self,name,age): #构造函数 self.name = name self.age = age print(sum1)#直接访问,报错 def do_homework(self): print(self.name +"do homework") student1 = Student("ansonw",26) student1.do_homework() print(Student.__dict__) # Project/python_ToolCodes/test6.py" # Traceback (most recent call last): # File "/Users/anson/Documents/Project/python_ToolCodes/test6.py", line 15, in <module> # student1 = Student("ansonw",26) # File "/Users/anson/Documents/Project/python_ToolCodes/test6.py", line 10, in __init__ # print(sum1) # NameError: global name 'sum1' is not defined
需要怎么访问呢:在类方法中,访问类变量,需要 采用内置变量__class__ 和关键字print(self.__class__.sum1)或者是print(self.sum1) 都可以访问,只是我认为,后面一种访问方法,是用当前对象访问的对象属性,不是类变量;__class__ 代指当前类
#coding=utf-8 class Student(): name = "" age = 0 sum1 = 0 def __init__(self,name,age): #构造函数 self.name = name self.age = age #print(sum1) print(self.__class__.sum1)#正确的访问方式 def do_homework(self): print(self.name +"do homework") # student1 = Student("ansonw",26) # student1.do_homework() print(Student.__dict__) #[Running] python -u "/Users/anson/Documents/Project/python_ToolCodes/test6.py" #0 #ansonwdo homework #{'__module__': '__main__', 'do_homework': <function do_homework at 0x10cbb3758>, 'name': '', 'age': 0, 'sum1': 0, '__doc__': None, '__init__': <function __init__ at 0x10cbb3140>}
4、实例方法访问实例变量,实例方法如何被调用?
#coding=utf-8 class Student(): name = "" age = 0 sum = 0 def __init__(self,name,age): #构造函数 self.name = name#实例方法访问实例变量 self.age = age def do_homework(self):#实例方法 print("name:"+self.name)#实例方法访问实例变量 print("this is a object function") @classmethod def plus_sum(cls):#类方法 cls.sum+=1 print(cls.sum) print(self.name) @staticmethod def add(x,y): print(x+y) print("this is a static method") student1 = Student("anson",19) student1.do_homework()#实例方法被调用(被对象调用) # [Running] python -u "/Users/anson/Documents/Project/python_ToolCodes/test6.py" # name:anson # this is a object function
5、类方法中能否访问实例变量?
#coding=utf-8 class Student(): name = "" age = 0 sum = 0 def __init__(self,name,age): #构造函数 self.name = name self.age = age def do_homework(self):#实例方法 print(self.name +"do homework") @classmethod def plus_sum(cls):#类方法 cls.sum+=1 print(cls.sum) print(self.name) @staticmethod def add(x,y): print(x+y) print("this is a static method") student1 = Student("anson",19) student1.plus_sum() Student.plus_sum()
报错
[Running] python -u "/Users/anson/Documents/Project/python_ToolCodes/test6.py" 1 Traceback (most recent call last): File "/Users/anson/Documents/Project/python_ToolCodes/test6.py", line 25, in <module> student1.plus_sum() File "/Users/anson/Documents/Project/python_ToolCodes/test6.py", line 16, in plus_sum print(self.name) NameError: global name 'self' is not defined
6、对象调用类方法:
student1.plus_sum()
虽然可以,但是,最好还是不要,因为不伦不类的
#coding=utf-8 class Student(): name = "" age = 0 sum = 0 def __init__(self,name,age): #构造函数 self.name = name self.age = age def do_homework(self):#实例方法 print(self.name +"do homework") @classmethod def plus_sum(cls):#类方法 cls.sum+=1 print(cls.sum) student1 = Student("a",18) student1.plus_sum() Student.plus_sum() student2 = Student("b",19) Student.plus_sum() student3 = Student("c",20) Student.plus_sum()
7、静态方法怎么定义&8、静态方法如何访问类变量、实例变量、静态方法如何被调用
同理,运用装饰器:@staticmethod
#coding=utf-8 class Student(): name = "" age = 0 sum = 0 def __init__(self,name,age): #构造函数 self.name = name self.age = age def do_homework(self):#实例方法 print(self.name +"do homework") @classmethod def plus_sum(cls):#类方法 cls.sum+=1 print(cls.sum) @staticmethod def add(x,y) pass
(1)静态方法访问类变量:
#coding=utf-8 class Student(): name = "" age = 0 sum = 0 def __init__(self,name,age): #构造函数 self.name = name self.age = age def do_homework(self):#实例方法 print(self.name +"do homework") @classmethod def plus_sum(cls):#类方法 cls.sum+=1 print(cls.sum) @staticmethod def add(x,y) print(Student.sum)#静态方法访问类变量 print("this is a static method")
调用静态方法:(1)可以用类名调用、(2)可以用实例来调用
#coding=utf-8 class Student(): name = "" age = 0 sum = 0 def __init__(self,name,age): #构造函数 self.name = name self.age = age def do_homework(self):#实例方法 print(self.name +"do homework") @classmethod def plus_sum(cls):#类方法 cls.sum+=1 print(cls.sum) @staticmethod def add(x,y): print(Student.sum)#静态方法访问类变量 print("this is a static method") Student.add(1,2)#(1)可以用类名调用 student1 = Student("anson",19) student1.add(1,2)#(2)可以用实例来调用 # [Running] python -u "/Users/anson/Documents/Project/python_ToolCodes/test6.py" # 0 # this is a static method # 0 # this is a static method
静态方法访问实例变量
#coding=utf-8 class Student(): name = "" age = 0 sum = 0 def __init__(self,name,age): #构造函数 self.name = name self.age = age def do_homework(self):#实例方法 print(self.name +"do homework") @classmethod def plus_sum(cls):#类方法 cls.sum+=1 print(cls.sum) @staticmethod def add(x,y): #print(Student.sum)#静态方法访问类变量 print(self.name) print("this is a static method") Student.add(1,2) student1 = Student("anson",19) student1.add(1,2)
报错
# [Running] python -u "/Users/anson/Documents/Project/python_ToolCodes/test6.py" # Traceback (most recent call last): # File "/Users/anson/Documents/Project/python_ToolCodes/test6.py", line 22, in <module> # Student.add(1,2) # File "/Users/anson/Documents/Project/python_ToolCodes/test6.py", line 19, in add # print(self.name) # NameError: global name 'self' is not defined