惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
博客园 - 聂微东
Application and Cybersecurity Blog
Application and Cybersecurity Blog
MyScale Blog
MyScale Blog
罗磊的独立博客
H
Help Net Security
L
LangChain Blog
T
Threat Research - Cisco Blogs
量子位
S
Securelist
Last Week in AI
Last Week in AI
L
Lohrmann on Cybersecurity
T
The Exploit Database - CXSecurity.com
P
Privacy International News Feed
The Hacker News
The Hacker News
Vercel News
Vercel News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Blog of Author Tim Ferriss
T
Threatpost
Security Latest
Security Latest
P
Palo Alto Networks Blog
Microsoft Security Blog
Microsoft Security Blog
NISL@THU
NISL@THU
F
Full Disclosure
WordPress大学
WordPress大学
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Stack Overflow Blog
Stack Overflow Blog
C
Check Point Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Heimdal Security Blog
J
Java Code Geeks
Recorded Future
Recorded Future
Hugging Face - Blog
Hugging Face - Blog
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
阮一峰的网络日志
阮一峰的网络日志
U
Unit 42
B
Blog RSS Feed
月光博客
月光博客
C
Cisco Blogs
V
Visual Studio Blog
D
DataBreaches.Net
H
Hacker News: Front Page
博客园 - 叶小钗
N
News and Events Feed by Topic
爱范儿
爱范儿
A
Arctic Wolf

博客园 - 西康的博客

mysql information_schema库中COLUMNS 和 TABLES表字段详解 python - queue 队列模块 python - 修改系统环境变量 python - threading 多进程模块 python - socketserver python - 反射 python - socket 基础 python 序列化 字典 异常处理 类的成员,类的特殊方法 elasticsearch RESTfull _cat api python - 面向对象编程基础知识 python 文件操作 python re 模块 正则表达式 python configparser 模块 python logging 模块 python hashlib 模块 python sys 模块
python - 面向对象编程基础知识 (进阶)
西康的博客 · 2018-04-01 · via 博客园 - 西康的博客

一、在类中的普通字段静态字段

class foo():
    c_name = 'cc'
    def __init__(self,content):
        self.content = content
    def show(self):
        print (self.content)
print (foo.c_name)
obj = foo('test content')
obj.show()
print (obj.c_name)
# test content
# cc
# cc

  在这段代码中c_name就是类中的静态字段,而在实例化foo时,传递的'test content'就是普通字段。普通字段保存在对象,并且普通字段只能通过对象访问,普通字段是可以被修改的。静态字段保存类,可以通过类或对象来访问。

  静态字段是在类实例化之前就已经在内存中了,他是python解释器自上而下执行的时候就已经运行了c_name = 'cc'。

二、在类中有三种方法

  1. 普通方法

    只能通过对象来调用。

    如果对象中需要保存一些值,同时在对象调用方法的时候需要用对象中的这些值,这时就用普通方法。

   2. 静态方法

    可以通过类或对象调用。

    静态方法就类似一个函数,不用通过创建对象直接调用。

    不需要任何对象的值,就要运行代码。则可以用静态方法。

  3. 类方法

    保存在类中,由类直接调用。cls是python自动传的。

    类方法和静态方法比较类似,但类方法可以使用类中的值。

class foo():
    def __init__(self,content):
        self.content = content
        print ('__init__ method')
    def show(self):
        print (self.content)
    @staticmethod
    def sm(str1,str2):
        print (str1,str2)

obj = foo('test content')
obj.show()
foo.sm('string1','string2')
obj.sm('string3','sting4')

 三、类中成员的属性

  定义类中的属性方法。

class foo():
    @property
    def pers(self):
        return 'pers'
    @pers.setter
    def pers(self,val):
        print (val)
    @pers.deleter
    def pers(self):
        print ('del pers')
obj = foo()
print (obj.pers)
obj.pers = 'test_string'
del obj.pers
# pers
# test_string
# del pers

   属性的另外一种写法

class bar():
    def b1(self):
        return 123
    def b2(self,val):
        print ('set is {}'.format(val))
    def b3(self):
        print ('del is True.')
    per = property(fget=b1,fset=b2,fdel=b3)
obj_bar = bar()
print (obj_bar.per)
obj_bar.per = 'test_string'
del obj_bar.per
# 123
# set is test_string
# del is True