python学习笔记之--类的三种方法

时间:2019-10-22
本文章向大家介绍python学习笔记之--类的三种方法,主要包括python学习笔记之--类的三种方法使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
方法:
    1)实例方法
    2)类方法
    3)静态方法
示例:
#encoding=utf-8
class Foo:


    def __init__(self, name):
        self.name = name


    def ord_func(self):
        """ 定义普通方法,至少有一个self参数 """


        # print self.name
        print ('普通方法')


    @classmethod
    def class_func(cls):
        """ 定义类方法,至少有一个cls参数 """


        print ('类方法')


    @staticmethod
    def static_func():
        """ 定义静态方法 ,无默认参数"""


        print ('静态方法')


f=Foo("吴老师")
f.ord_func()
Foo.class_func()
f.class_func()
Foo.static_func()
f.static_func()
#实例方法:参数要有self
实例方法使用的时候必须要实例化
调用方法:
    ①只能通过实例来调用,实例.get_name()
思考:实例方法为什么不能通过类名来调用?Person.get_name()-----调用的时候会传一个self,而用类来调用没有实例化self找不到对象地址
class Person:
 
    def __init__(self,name,gender):
        self.name = name
        self.gender = gender
 
    def get_name(self): #实例方法,必须要实例化才能使用
        return self.name

#调用实例方法的第一种写法:直接用类名+实例化调用
print(Person("吴老师","Male").get_name()) #但是这种方法实例没有存到变量里,所以只能使用一次
#调用实例方法的第二种写法:1.先做实例化;2.用实例名+方法名
wulaoshi = Person("吴老师","Male") #实例化
print(wulaoshi.get_name())
#类方法:用classmethod来声明类方法,需要加默认参数cls
类方法使用的时候不需要实例化
调用方法:
    ①通过类名来使用,类名.get_instance_count()
    ②也可以通过实例调用,实例对象.get_instance_count()
class Person:
    count = 0 #类变量
 
    def __init__(self,name,gender):
        self.name = name
        self.gender = gender
        Person.count +=1
 
    def get_name(self):
        return self.name
 
    #类方法:可以使用类变量,不能使用实例变量-----这是为什么呢?:参数没有self,找不到实例的地址,因此不能用实例变量
    @classmethod  #加classmethod才能标识为类方法
    def get_instance_count(cls):
        return Person.count
 
    @classmethod
    def create_a_instance(cls):
        return Person("","")   #类方法里虽然不可以使用实例变量,但是可以创建实例
 
print(Person.count)
Person("吴老师","Male")
print(Person.count)
print(Person.get_instance_count()) #用类调用
print(Person("吴老师","Male").get_instance_count()) #用实例调用
C:\Users\dell\Desktop\练习\6>py -3 0616.py
0
1
1
2
#静态方法:用staticmethod来声明,不需要加默认参数self和cls
调用方法:
    ①类名调用,类名.get_nation()
    ②实例来调用,实例.get_nation()
class Person:
    count = 0 #类变量
    nation = "中国"
 
    def __init__(self,name,gender):
        self.name = name
        self.gender = gender
        Person.count +=1
 
    def get_name(self):#实例方法,必须要实例化
        return self.name
 
 
    @staticmethod   #静态方法:不需要self和cls
    def get_nation():
        return Person.nation
        
 
 
print(Person.get_nation())  #类名调用
print(Person("吴老师","Male").get_nation())  #实例调用
总结:
三种方法的区别:
1 实例方法,参数要有self,必须通过实例化的对象去调用。
2 类方法,要加上@classmethod来声明,参数至少有一个,一般定义为cls,使用类变量,不能使用实例变量。通过类名或者实例对象调用。
3 静态方法,要加上@staticmethod来声明,可以没有参数,使用类变量,不能使用实例变量。通过类名或者实例对象调用。
什么时候该使用哪种方法?
不想做实例化,且只操作类变量:类方法、静态方法
如果想使用实例变量,只能使用实例方法了。
#一个静态方法应用的场景例子:
class FileUtil:


    @staticmethod
    def get_file_name(file_path):
        with open(file_path) as fp:
            return fp.name


    @staticmethod
    def get_file_content(file_path):
        with open(file_path,encoding="utf-8") as fp:
            return fp.read()




print(FileUtil.get_file_name("e:\\a.txt"))
print(FileUtil.get_file_content("e:\\a.txt"))

原文地址:https://www.cnblogs.com/wenm1128/p/11716219.html