Python中的类和方法使用举例,,1.类的属性成员变量


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.__ageren = People()ren.color = ‘白色人‘print ren.colorren.think()print ‘#‘*30print("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.__agedef  __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.__agedef  __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.__agedef  __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中的类和方法使用举例

评论关闭