定义类型
class Fish:
def __init__(self,x):
self.num = x
class Turtle:
def __init__(self,y):
self.num = y
class Pool:
def __init__(self,x,y):
self.fish = Fish(x)
self.turtle = Turtle(y)
def print_num(self):
print('fish %d,trutle %d'%(self.fish.num,self.turtle.num))
#pool = Pool(10,1)
#pool.print_num()
创建使用自定义类型的方法
import Animal as al
from Animal import Fish
def PrintTest(X):
for colour in X:
print(colour)
def PrintAnl(Fish):
print(Fish.num)
def PrintFish(fish=Fish):
print(fish.num)
调用
import PrintTest as pt
import Animal as anl
pt.PrintTest(['1','2'])
fish = anl.Fish(5)
pt.PrintAnl(fish)
pt.PrintFish(fish)
pool = anl.Pool(1,2)
pool.print_num()
拙见:向方法中传递自定类型参数时,格式:方法名(自定义类型,自定义类型) 或者 方法名(参数名=自定义类型,
参数名=自定义类型
)。传递内置类型时Python自动会判断类型