设为首页 加入收藏

TOP

静态方法、类方法、属性方法(一)
2017-09-30 17:31:06 】 浏览:1897
Tags:静态 方法 属性

一、静态方法

 1.1、定义

   在类中的方法前面通过@staticmethod装饰器即可把其装饰的方法变为一个静态方法

class Person(object):

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

    @staticmethod
    def speak():
        print('someone is speaking chinese.')

# 静态方法在类中也不需要传入 self参数

  

1.2、静态方法的特性

  静态方法是不能访问实例变量和类变量的 

class Person(object):

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

    @staticmethod
    def speak(self):
        print('%s is speaking chinese.' % self.name)

p = Person('Bigberg')
p.speak()


# 我们在 speak(self) 函数中传入 self

  事实上以上代码运行会出错的,说speak 需要一个self参数,但调用时却没有传递,没错,当speak变成静态方法后,再通过实例调用时就不会自动把实例本身当作一个参数传给self了。  

Traceback (most recent call last):
  File "G:/python/untitled/study6/静态方法.py", line 26, in <module>
    p.speak()
TypeError: speak() missing 1 required positional argument: 'self'

  想让以上代码可以正常执行,有两种方法:

  1. 在调用时将实例本身传给 speak() 
class Person(object):

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

    @staticmethod
    def speak(self):
        print('%s is speaking chinese.' % self.name)

p = Person('Bigberg')
p.speak(p)

# 输出

Bigberg is speaking chinese.

  2.在方法speak中去掉self,但这也意味着,在eat中不能通过self.调用实例中的其它变量了 

class Person(object):

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

    @staticmethod
    def speak():                # 方法中已经没有 self 参数了
        print('%s is speaking chinese.' % 'anyone')

p = Person('Bigberg')
p.speak()


#输出
anyone is speaking chinese.

  

1.3 总结

  普通的方法,可以在实例化后直接调用,并且在方法里可以通过self.调用实例变量或类变量,但静态方法是不可以访问实例变量或类变量的,一个不能访问实例变量和类变量的方法,其实相当于跟类本身已经没什么关系了,它与类唯一的关联就是需要通过类名来调用这个方法。

 

二、类方法

  2.1、定义

  类方法通过@classmethod装饰器实现,类方法和普通方法的区别是, 类方法只能访问类变量,不能访问实例变量

  2.2、访问实例变量

        直接访问实例变量会报错,没有该属性  

class Person(object):

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

    @classmethod
    def nationality(self):
        print('Bigberg is %s.' % self.country)

p = Person('Bigberg', 'CN')
p.nationality()

# 输出
Traceback (most recent call last):
  File "G:/python/untitled/study6/静态方法.py", line 31, in <module>
    p.nationality()
  File "G:/python/untitled/study6/静态方法.py", line 24, in nationality
    print('Bigberg is %s.' % self.country)
AttributeError: type object 'Person' has no attribute 'country'

# 提示没有一个 country 属性  

  2.3、访问类变量,即 全局属性/静态字段 

class Person(object):

    country = 'Chinese'    # 增加一个 全局属性/静态字段

    def __init__(self, name, country):

        self.name = name
        self.country = country

    @classmethod
    def nationality(cls):    # 这里将sefl 改为 cls
        print('Bigberg is %s.' % cls.country)

p = Person('Bigberg', 'CN')
p.nationality()

# 输出
Bigberg is Chinese.

 

三、属性方法 

   3.1、定义

  属性方法的作用就是通过@property把一个方法变成一个静态属性 

class Person(object):

    country = 'Chinese'

    def __init__(self, name, country):

        self.name = name
        self.country = country

    @property
    def drive(self):
        print('%s is driving a car.' % self.name)
p = Person('Bigberg', 'CN')
p.drive()
# 输出 
Traceback (most recent call last): Bigberg is driving a car. File "G:/python/untitled/study6/静态方法.py", line 38, in <module> p.drive() TypeError: 'NoneType' object is not callable

  调用会出错误, 说NoneType is not callable, 因为eat此时已经变成一个静态属性了, 不是方法了, 想调用已经不需要加()号了,直接p.drive就可以了

  正常调用: 

p = Person('Bigberg', 'CN')
p.drive

# 输出

Bigberg is driving a car.

  

  3.2 setter用法

  如果我们想在属性方法里传参,比如车的品牌,我们就要用setter了,具体用法  @属性方法名.setter 

class Person(object):

    country = 'Chinese'

    d
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇python008 Python3 字符串 下一篇day6面向对象--继承、多态

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目