#coding=utf-8
#这就是一个obj与实例化的游戏
'''
class test():
def __init__(self):
print "hello world!"
a = test #此时a是classobj
print type(a)
b = a() #此时b才是实例化
print type(b)
b
'''
#上面这个例子,是classobj与实例化的简单模板
'''
class Bike():
def mybike(self):
print "hello world"
self.electronic_bike = 'Blue ELITE'
class Car():
def __init__(self,myclass):
self.myclass = myclass
print type(myclass) #此时证明:self.myclass = myclass,会让myclass成为classobj
def test(self):
self.money = self.myclass() #此时证明:实例化
print "it's test!!!!"
a = Car(Bike)
a.test()
print type(a.test)
print type(a.money)
print type(a.myclass)
a.money.mybike()
print a.money.electronic_bike
'''
'''
class Bike():
print "hahaha"
ibike = "Hehehehe"
def Py(self):
print "hello world"
self.ebike = 'Blue Ebike'
class Car():
def __init__(self,myclass):
self.myclass = myclass
def test(self): #test为类的method
self.myclass() #此时证明:self.myclass为classobj
a = Car(Bike)
a.test()
print type(a.test)
print type(a.myclass)
b = a.myclass()
b.Py()
'''
烧脑子的“实例化一个类时,同时将另一个类作为参数”。
----------------------------------------------------分割一下吧-------------------------------------------------------------------------
在脑子烧毁之前,再来一记“在一个类中,引用另一个类的方法”:
class Car():
def __init__(self,tmd):
self.tmd = tmd
def run(self):
print self.tmd.name
class City():
def __init__(self,name):
self.name = name
def buyCar(self):
self.car = Car(self)
def run(self):
self.car.run()
city = City('shanghai')
city.buyCar()
city.run()
city = City('beijing')
city.buyCar()
city.run()
以上这些怪异的用法,均来自对SocketServer模块源代码中的疑惑。