python系统学习第六天

类的函数分类:
类函数…@classmethod 放在函数上面,用来标记/装饰,可以通过类和对象来访问

静态函数…@staticmethod 放在函数上面,来标记/装饰,可以通过类和对象来访问

对象函数…只能通过对象来调用的方法
创建一个类,类里面有属性,方法,对象

对象的创建 ---->类名()
类和对象可以直接访问属性,并获取他们的值
类的方法和普通函数有啥区别呢?
除了类的方法里面有self之外,其他都没有区别


    @staticmethod #静态方法  
    def music():
     print("music=====")
    def send_messege(self):
        Phone.music() #静态方法的调用
        print("send messege===========")
    @classmethod
    def call_back(cls): #类方法
        cls.music()  #类方法的调用
        Phone.music() #类方法的调用
        print("call back===============")
    def contact(self):
        print("contact================")
    def dysx(self):
        print("调用属性,tel={},call={}".format(self.tel,self.call))
p=Phone()
p.music()
p.dysx()
p.send_messege()
p.call_back()
结果
music=====
调用属性,tel=1888888888888,call=call属性
music=====
send messege===========
music=====
music=====

初始化函数:
初始化函数:
def——init——(self):
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/guotianxiu1234/article/details/89348778
今日推荐