动态方法与静态对象


class Alex:
xue = "劳动"
def __init__(self,name,age):
self.name = name
self.age = age


p2 = Alex('小李',16)
print (p2.name)

#动态方法,需要有self:
class MSsqlhelper:
def add(self,sql):
print ("add %s success!"%sql)
pass
def delete(self,sql):
print ("del %s success!"%sql)
pass
def update(self,sql):
pass
def select(self,sql):
pass

MS = MSsqlhelper() ##创建对象
MS.add("667788")
MS.delete("5555")

#静态对象的作用(不需要self,但要加staticmethod):
class MSsqlhelper2:
@staticmethod
def add(sql):
print ("add %s success!" %sql)
pass
@staticmethod
def delete(sql):
print (sql)
pass
@staticmethod
def update():
pass
@staticmethod
def select(sql):
pass

MSsqlhelper2.add("9999")
MSsqlhelper2.delete("del877")
print ("_____________________")
原文地址:https://www.cnblogs.com/liulvzhong/p/7609746.html