定义类型
class Fish: def __init__(self,x): self.num = xclass Turtle: def __init__(self,y): self.num = yclass 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 alfrom Animal import Fishdef PrintTest(X): for colour in X: print(colour)def PrintAnl(Fish): print(Fish.num)
def PrintFish(fish=Fish): print(fish.num)
调用
import PrintTest as ptimport Animal as anlpt.PrintTest([‘1‘,‘2‘])fish = anl.Fish(5)pt.PrintAnl(fish)pt.PrintFish(fish)pool = anl.Pool(1,2)pool.print_num()拙见:向方法中传递自定类型参数时,格式:方法名(自定义类型,自定义类型) 或者 方法名(参数名=自定义类型,
参数名=自定义类型
)。传递内置类型时Python自动会判断类型
时间: 2024-10-22 06:55:13