类和对象
class dog:
eye_num=2
def __init__( self,color, type, name):
self.color = color
self.type = type
self.name = name
def cry(self):
print('%s正在叫'%(self.name))
a = dog('yellow','哈士奇','哈哈')
a.cry()
class house:
def __init__( self,length ,wide):
self.length= length
self.wide = wide
@property. #在一个方法上面加上`@property`就变成了静态属性
def area(self):
return self.length*self.wide
a=house(20,20)
print(a.area)
- 类方法
一个方法不需要实例就可以调用的方法,但实例还是可以调用这个方法。而且类方法只能使用类属性
class house:
wide=13
def __init__( self,length ,wide):
self.length= length
self.wide = wide
@property
def area(self):
return self.length*self.wide
@classmethod
def a(cls):
print(cls.wide)
def b(self):
print('b')
house.a().
house.b()
b=house(12,22)
house.b(b)
b.a()
- 静态方法
静态方法只是名义上归属类管理,不能使用类属性和实例属性。和类方法一样也可以 通过类名来调用
class house:
wide=13
def __init__( self,length ,wide):
self.length= length
self.wide = wide
@property
def area(self):
return self.length*self.wide
@classmethod
def a(cls):
print(cls.wide)
@staticmethod
def b():
print('b')
house.b()