day07_02 类的静态方法 @staticmethod

__author__ = "Alex Li"

import os
# os.system()
# os.mkdir()

# class Dog(object):
#     def __init__(self,name):
#         self.name = name
#
#     # 实际上跟类没什么关系了,不传递self调用,是普通的函数,不是类方法
#     def eat(self):
#         print("%s is eating %s" %('self.name','dd'))
#
#     def talk(self):
#         print("%s is talking"% self.name)
#
#
# d = Dog("ChenRonghua")
# d.eat()
# d.talk()

class Dog(object):
    def __init__(self,name):
        self.name = name

    # 实际上跟类没什么关系了,不传递self调用,是普通的函数,不是类方法
    @staticmethod
    def eat(self):
        print("%s is eating %s" %('self.name','dd'))

    def talk(self):
        print("%s is talking"% self.name)


d = Dog("ChenRonghua")
#将实例地址传入函数,是普通的函数d.eat()报错
d.eat(d)
d.talk()
posted @ 2021-06-06 12:02  flytoyou  阅读(21)  评论(0编辑  收藏  举报