zoukankan      html  css  js  c++  java
  • python学习笔记之--类的三种方法

    在类的内部,使用 def 关键字来定义一个方法。
    方法:
        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:UsersdellDesktop练习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"))
  • 相关阅读:
    Python中的list和tuple
    Python中输出格式化的字符串
    Python笔记-第一天
    在Lingo中输入矩阵(通过Excel)
    将Matlab中的矩阵输出到txt文件
    SQL中对于两个不同的表中的属性取差集except运算
    SQL中union运算操作的理解
    SQL笔记----在一个关系表中操作列
    MathType的公式在word中跟文字不对齐
    开发android过程中eclipse闪退解决
  • 原文地址:https://www.cnblogs.com/wenm1128/p/11716219.html
Copyright © 2011-2022 走看看