class MyClass(object):
message = 'Hello, Developer.'
def show(self):
print self.message
def __init__(self, name = "unset", color = "black"):
print "Constructor is called with params: ",name, " ", color
def __del__(self):
print "Destructor is called!"
inst = MyClass()
inst.show()
inst2 = MyClass("David")
inst2.show()
del inst, inst2
inst3 = MyClass("Lisa", "Yellow")
inst3.show()
del inst3
>>>
打印结果:
Constructor is called with params: unset black
Hello, Developer.
Constructor is called with params: David black
Hello, Developer.
Destructor is called!
Destructor is called!
Constructor is called with params: Lisa Yellow
Hello, Developer.
Destructor is called!
4.实例成员变量
构造函数中定义self引用的变量,因此这样的成员变量在python中叫做实例成员变量。
def __init__(self, name = "unset", color = "black"):
print "Constructor is called with params: ",name, " ", color
self.name = name
self.color = color
class MyClass(object):
message = 'Hello, Developer.'
def show(self):
print (self.message)
print ("Here is %s in %s!" % (self.name, self.color))
@staticmethod
def printMessage():
print ("printMessage is called")
print (MyClass.message)
@classmethod
def createObj(cls, name, color):
print ("Object will be created: %s(%s, %s)"% (cls.__name__, name, color))
return cls(name, color)
def __init__(self, name = "unset", color = "black"):
print ("Constructor is called with params: ",name, " ", color)
self.name = name
self.color = color
def __del__(self):
print ("Destructor is called for %s!"% self.name)
MyClass.printMessage()
inst = MyClass.createObj( "Toby", "Red")
print (inst.message)
del inst
输出结果:
printMessage is called
Hello, Developer.
Object will be created: MyClass(Toby, Red)
Constructor is called with params: Toby Red
Hello, Developer.
Destructor is called for Toby!
class MyClass(object):
def __init__(self, name = "unset", color = "black"):
print "Constructor is called with params: ",name, " ", color
self.__name = name
self.__color = color
def __del__(self):
print "Destructor is called for %s!"% self.__name
inst = MyClass("Jojo", "White")
del inst
输出结果:
Constructor is called with params: Jojo White
Destructor is called for Jojo!
class MyClass(object):
message = 'Hello, Developer.'
def show(self):
print self.message
def __init__(self, name = "unset", color = "black"):
print "Constructor is called with params: ",name, " ", color
def __del__(self):
print "Destructor is called!"
inst = MyClass()
inst.show()
inst2 = MyClass("David")
inst2.show()
del inst, inst2
inst3 = MyClass("Lisa", "Yellow")
inst3.show()
del inst3
>>>
打印结果:
Constructor is called with params: unset black
Hello, Developer.
Constructor is called with params: David black
Hello, Developer.
Destructor is called!
Destructor is called!
Constructor is called with params: Lisa Yellow
Hello, Developer.
Destructor is called!
4.实例成员变量
构造函数中定义self引用的变量,因此这样的成员变量在python中叫做实例成员变量。
def __init__(self, name = "unset", color = "black"):
print "Constructor is called with params: ",name, " ", color
self.name = name
self.color = color
class MyClass(object):
message = 'Hello, Developer.'
def show(self):
print (self.message)
print ("Here is %s in %s!" % (self.name, self.color))
@staticmethod
def printMessage():
print ("printMessage is called")
print (MyClass.message)
@classmethod
def createObj(cls, name, color):
print ("Object will be created: %s(%s, %s)"% (cls.__name__, name, color))
return cls(name, color)
def __init__(self, name = "unset", color = "black"):
print ("Constructor is called with params: ",name, " ", color)
self.name = name
self.color = color
def __del__(self):
print ("Destructor is called for %s!"% self.name)
MyClass.printMessage()
inst = MyClass.createObj( "Toby", "Red")
print (inst.message)
del inst
输出结果:
printMessage is called
Hello, Developer.
Object will be created: MyClass(Toby, Red)
Constructor is called with params: Toby Red
Hello, Developer.
Destructor is called for Toby!
class MyClass(object):
def __init__(self, name = "unset", color = "black"):
print "Constructor is called with params: ",name, " ", color
self.__name = name
self.__color = color
def __del__(self):
print "Destructor is called for %s!"% self.__name
inst = MyClass("Jojo", "White")
del inst
输出结果:
Constructor is called with params: Jojo White
Destructor is called for Jojo!