设为首页 加入收藏

TOP

Python中类的定义与使用(二)
2018-10-06 21:34:59 】 浏览:179
Tags:Python 定义 使用
' of 'Person' objects>, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None, 'arm': 1}
4--删除类的属性: {'__module__': '__main__', '__init__': <function Person.__init__ at 0x000001A8F769B6A8>, 'say': <function Person.say at 0x000001A8F769B730>, '__dict__': <attribute '__dict__' of 'Person' objects>, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None, 'arm': 1}


看代码应该就差不多明白类属性的操作了。


注:类属性一般一经定义,不会在执行的过程中增加、删除类的属性


定义类属性一般只用第一种方法,其它的骚操作了解就好,忘了它吧


2、实例属性的增删改查


class Person(object):


    def __init__(self, name):
        # 实例属性添加第一种方式
        self.name = name


    def say(self, w):
        print("%s说了%s" % (self.name, w))



p1 = Person("张三")
p2 = Person("李四")
print("1--p1的属性:", p1.__dict__)
print("1--p2的属性:", p2.__dict__)


# 实例属性添加第二种方式
p1.age = 20
print("2--p1的属性:", p1.__dict__)
print("2--p2的属性:", p2.__dict__)


# 删除实例属性
del p1.name
print("3--p1的属性:", p1.__dict__)
print("3--p2的属性:", p2.__dict__)


# 修改实例属性
p1.age = 10
print("4--p1的属性:", p1.__dict__)
print("4--p2的属性:", p2.__dict__)



# 输出结果
1--p1的属性: {'name': '张三'}
1--p2的属性: {'name': '李四'}
2--p1的属性: {'name': '张三', 'age': 20}
2--p2的属性: {'name': '李四'}
3--p1的属性: {'age': 20}
3--p2的属性: {'name': '李四'}
4--p1的属性: {'age': 10}
4--p2的属性: {'name': '李四'}


实例属性跟类属性的操作差不多。从上也可以得出结论,对对象 p1 的操作并不影响 p2 的属性。


注:实例属性一般放在init方法里边初始化,不会在执行的过程中增加、删除对象的属性


三、方法


1、普通的方法


上边没有@符号修饰,供外部实例调用,普通的方法也叫实例方法,但虽然叫实例方法但却与类相关,存储在类的属性中


2、类方法


上边有@classmethod修饰,定义时第一个参数必须为"cls",并通过cls来调用类属性,无法访问实例属性


3、静态方法()


上边有@staticmethod,与类相关但不需要访问属性,无法调用其它方法与属性


class Person(object):
    eyes = 2


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


    def say(self, w):  # 普通方法
        print("%s say %s" % (self.name, w))


    @classmethod
    def cls_md(cls):  # 类方法
        print("这是类方法", cls.eyes)


    @staticmethod
    def stat():  # 静态方法
        print("这是静态方法")



p1 = Person("zhangs")
print(Person.__dict__)
p1.say("hello")
p1.cls_md()
p1.stat()


# 输出结果
{'__module__': '__main__', 'eyes': 2, '__init__': <function Person.__init__ at 0x000001DF5205B6A8>, 'say': <function Person.say at 0x000001DF5205B730>, 'cls_md': <classmethod object at 0x000001DF5205ABE0>, 'stat': <staticmethod object at 0x000001DF5205AEB8>, '__dict__': <attribute '__dict__' of 'Person' objects>, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None}
zhangs say hello
这是类方法 2
这是静态方法


4、方法的增(了解即可)


class Person(object):
    eyes = 2


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



def say2(self):  # 普通方法
    print("%s 增加普通方法" % (self.name))



@classmethod
def ha2(cls):  # 类方法
    print("增加类方法", cls.eyes)



@staticmethod
def stat2():  # 静态方法
    print("增加静态方法")



print("增加前:", Person.__dict__)
Person.say2 = say2
Person.ha2 = ha2
Person.stat2 = stat2
print("增加后:"

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇5分钟学会 Python 装饰器 下一篇OpenCL:图像处理基础note

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目