属性:
公有属性 (属于类,每个类一份)
普通属性 (属于对象,每个对象一份)
私有属性 (属于对象,跟普通属性相似,只是不能通过对象直接访问)
方法:(按作用)
构造方法
析构函数
方法:(按类型)
普通方法
私有方法(方法前面加两个下划线)
静态方法
类方法
属性方法
静态方法
@staticmethod
静态方法,通过类直接调用,不需要创建对象,不会隐式传递self
类方法
@classmethod
类方法,方法中的self是类本身,调用方法时传的值也必须是类的公有属性,
就是说类方法只能操作类本身的公有字段
classDog(object):
food= "gutou"age= "1"
def __init__(self, name):
self.NAME=name
@classmethoddef eat(self,age): #只能是类中的变量
#print(self.NAME)
print(age)print(self.food)
@classmethoddef eat1(self, age): #只能是类中的变量
#print(self.NAME)
age = "2"self.food= "tang"@staticmethoddefprint_1():print(Dog.food, Dog.age)
d= Dog("labuladuo")
d.eat(Dog.age)#通过对象调用
Dog.eat(Dog.age) #通过类调用
print("-----1-----")
d.eat1(Dog.age)
Dog.print_1()print("--------2-------")
Dog.eat1(Dog.age)
Dog.print_1()
output:
1
gutou
1
gutou
-----1-----
('tang', '1')
--------2-------
('tang', '1')