网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Python中的类和方法使用举例

1.类的属性

成员变量
对象的创建
创建对象的过程称之为实例化,当一个对象被创建后,包含三个方面的特性对象聚丙属性和方法,
句柄用于区分不同的对象,
对象的属性和方法,与类中的成员变量和成员函数对应,
obj = MyClass()创建类的一个实例,扩号对象,通过对象来调用方法和属性

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:申请域名网站空间、营销软件、网站建设、科尔沁右翼中网站维护、网站推广。

类的属性

类的属性按使用范围分为公有属性和私有属性类的属性范围,取决于属性的名称,
共有属性---在内中和内外都能够调用的属性
私有属性---不能在内外贝类以外函数调用
定义方式:以""双下划线开始的成员变量就是私有属性
可以通过instance.
classnameattribute方式访问,
内置属性--由系统在定义类的时候默认添加的由前后双下划线构成,如
dic,module__

#!/usr/bin/env python
#-*- coding:utf-8  -*-
class People(object):
    color = 'yellow'
     __age = 30   #私有属性

    def think(self):
        self.color = "black"
        print "I am a %s "  % self.color
        print ("I am a thinker")
        print self.__age

ren = People()
ren.color = '白色人'
print ren.color
ren.think()
print '#'*30
print("People.color")
print ren.__People__age  ##测试时使用。如要调用 时,通过方法内调用 。

2.类的方法

成员函数

类的方法
方法的定义和函数一样,但是需要self作为第一个参数.
类方法为:
公有方法
私有方法
类方法
静态方法

公有方法:在类中和类外都都测调用的方法.
私有方法:不测被类的外部调用模块,在方法前加个“__”c双下划线就是私有方法。
self参数:
用于区分函数和类的方法(必须有一个self)
self参数表示执行对象本身

#!/usr/bin/env python
#-*- coding:utf-8  -*-
class People(object):
    color = 'yellow'
     __age = 30   #私有属性

    def think(self):
        self.color = "black"
        print "I am a %s "  % self.color
        print ("I am a thinker")
        print self.__age

   def test(self):

self.think() # 内部调用
jack = People()
jack.test()    #外部调用
#!/usr/bin/env python
#-*- coding:utf-8  -*-
class People(object):
    color = 'yellow'
     __age = 30   #私有属性

    def think(self):
        self.color = "black"
        print "I am a %s "  % self.color
        print ("I am a thinker")
        print self.__age

def  __talk(self):
print "I am talking with Tom"

 def test(self):
self.__talk() # 内部调用talk()

jack = People()
jack.test()    #外部调用

类方法
类方法:被classmethod()函数处理过的函数,能被类所调用,也能被对象所调用(是继承的关系)。

静态方法:相当于“全局函数”,可以被类直接调用,可以被所有实例化对象共享,通过staticmethod()定义静态方法, 静态方法没有self参数

装饰器:@classmethod()
br/>@classmethod()

#!/usr/bin/env python
#-*- coding:utf-8  -*-
class People(object):
    color = 'yellow'
     __age = 30   #私有属性

    def think(self):
        self.color = "black"
        print "I am a %s "  % self.color
        print ("I am a thinker")
        print self.__age

def  __talk(self):
print "I am talking with Tom"

 def test(self):
print 'Testing....'

  cm = classmethod(test)

jack = People()
People.cm()
通过类方法类内的方法 ,不涉及的属性和方法 不会被加载,节省内存,快。 

#!/usr/bin/env python
#-*- coding:utf-8  -*-
class People(object):
    color = 'yellow'
     __age = 30   #私有属性

    def think(self):
        self.color = "black"
        print "I am a %s "  % self.color
        print ("I am a thinker")
        print self.__age

def  __talk(self):
print "I am talking with Tom"

 def test():   ##没有self   静态调用     会把所有的属性加载到内存里。
print People.__age   #  通过类访问内部变量

  sm = staticmethod(test)

jack = People()
People.sm()

装饰调用类的方法:

#!/usr/bin/env python
#-*- coding:utf-8  -*-
class People(object):
    color = 'yellow'
    __age = 30   #私有属性

    def think(self):
        self.color = "black"
        print "I am a %s "  % self.color
        print ("I am a thinker")
        print self.__age

    def  __talk(self):
        print "I am talking with Tom"

    @classmethod #调用类的方法 
    def test(self):
        print ("this is class method")

    @staticmethod  #调用类的方法 
    def test1():    
        print ("this is static method")

jack = People()
People.test()
People.test1()

网站题目:Python中的类和方法使用举例
网页链接:http://bjjierui.cn/article/pgpjih.html

其他资讯