zoukankan      html  css  js  c++  java
  • 实例方法、类方法和静态方法

    Python中常用的三种方法:

            1. 实例方法 (常见函数)

            2. 静态方法 (@staticmethod)

            3. 类方法  (@classmethod)

    实例方法

        定义:第一个参数必须是实例对象,该参数名一般约定为“self”,通过它来传递实例的属性和方法(也可以传类的属性和方法);

        调用:只能由实例对象调用。

    类方法

        定义:使用装饰器@classmethod。第一个参数必须是当前类对象,该参数名一般约定为“cls”,通过它性来传递类的属和方法(不能传实例的属性和方法);

        调用:实例对象和类对象都可以调用。

    静态方法

        定义:使用装饰器@staticmethod。参数随意,没有“self”和“cls”参数,但是方法体中不能使用实例的任何属性和方法,使用类的属性和方法需要使用类对象引用;

        调用:实例对象和类对象都可以调用。

    一、区别

      

    二、代码示例

      

    #!/usr/bin/env python  
    # -*- coding:utf-8 _*-
    __author__ = '池偏一'
     
    '''
        @author:shz
        @license: Apache Licence
        @file: testargs.py
        @time: 2018/06/19
        @site:
        @software: PyCharm
    '''
     
    import sys
     
    reload(sys)
    sys.setdefaultencoding('utf-8')
     
     
    class Test(object):
        # 测试类属性
        name = 'jwh'
     
        # 测试实例属性
        def __init__(self):
            self.age = 30
            self.address = '上海'
     
        # 测试私有实例方法
        def __instances(self):
            print "this is test instance"
     
        # 测试实例方法
        def instances_pub(self):
            school = '复旦大学'
            print school
     
        @classmethod
        def testclass(cls):
            job = 'dev'
            print job
     
        # -*-------------------------*- #
     
        # 该实例方法调用其他测试内容,看是否能成功
        def func(self):
            print Test.name  # 传递类属性
            print self.name  # 传递实例属性
            print self.age  # 传递实例属性
            self.__instances()  # 传递实例方法
            self.instances_pub()  # 传递实例方法
            self.testclass()  # 传递类方法
     
        @classmethod
        def class_func(cls):
            print cls.name  # 传递类属性
            cls.testclass()  # 传递类方法
            Test.testclass()  # 传递类方法
     
            try:
                print cls.address
            except AttributeError:
                print '类方法不能传递实例属性'
     
            try:
                cls.__instances()  # 传递实例方法
            except TypeError:
                print '类方法不能传递实例方法'
     
            try:
                cls.instances_pub()  # 传递实例方法
            except TypeError:
                print '类方法不能传递实例方法'
     
        @staticmethod
        def static_func():
            try:
                print Test.name  # 传递类属性
            except AttributeError:
                print '静态方法不能传递类属性'
     
            try:
                Test.testclass()  # 传递类方法
            except:
                print '静态方法不能传递类方法'
     
            try:
                print Test.address  # 传递实例属性
            except AttributeError:
                print '静态方法不能传递实例属性'
            try:
                Test.__instances()  # 传递实例方法
            except TypeError:
                print '静态方法不能传递实例方法'
            try:
                Test.instances_pub()  # 传递实例方法
            except TypeError:
                print '静态方法不能传递实例方法'
     
        # -*-------------------------*- #
    

      2.1  测试实例方法的传递和实例对象调用  

    try:
        t = Test()
        t.func()
    except TypeError, e:
        print "不能使用实例对象调用"
    

      2.2 测试实例方法的传递和类对象的调用

    try:
        Test.func()
    except TypeError, e:
        print "不能使用类对象调用"  

      2.3 测试类方法的传递和实例对象调用

    try:
        t = Test()
            t.class_func()
    except TypeError, e:
            print '不能使用实例对象调用'  

      2.4 测试类方法的传递和类对象调用

    try:
        Test.class_func()
    except TypeError, e:
        print '不能使用类对象调用'
    

      2.5 测试静态方法的传递和实例对象调用

    try:
        t = Test()
        t.static_func()
    except TypeError:
        print "不能使用实例对象调用"
    

      2.6 测试静态方法的传递和类对象调用

    try:
        Test.static_func()
    except TypeError:
        print "不能使用类对象调用"
    

  • 相关阅读:
    Window—mysql下载及安装
    postgresql 在windows下启动调试功能
    FASTREPORT自动换行及行高自适应
    如何卸载已经安装在delphi7中控件包?
    cxgrid使用三问1cxgrid 如何动态创建列2cxGrid 通过字段名取得列3cxGrid动态创建的列里动态创建事件的方法
    VirtualBox中Linux设置共享文件夹
    Android & iOS 启动画面制作工具(转自龟山Aone)
    PostgreSQL 基本数据类型及常用SQL 函数操作
    win10 安装Postgresql 服务不能启动报错
    TdxDbOrgChart 图标显示问题
  • 原文地址:https://www.cnblogs.com/cpy-devops/p/9287340.html
Copyright © 2011-2022 走看看