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()

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页题目:Python中的类和方法使用举例-创新互联
URL分享:http://scyanting.com/article/dppsdg.html