温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python中的类和方法使用举例

发布时间:2020-06-11 21:18:49 来源:网络 阅读:489 作者:枫叶云 栏目:编程语言

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()
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI