您可以使用一些默认参数,并根据填充的参数来决定使用哪个参数:class Block(object):
def __init__(self, height, width, coord_tuple=None, coord_x=None, coord_y=None):
if coord_tuple:
self.x = coord_tuple[0]
self.y = coord_tuple[1]
else:
self.x = coord_x
self.y = coord_y
self.height = height
self.width = width
return
def print_coord(self):
print("Height: {}\nWidth: {}\nX: {}\nY: {}"
.format(self.height, self.width, self.x, self.y))
return
# tuple instantiations
b1 = Block(1, 2, (1,2))
b2 = Block(3, 4, coord_tuple=(3,4))
# x/y instantiation
b3 = Block(5, 6, coord_x=5,coord_y=6)
b1.print_coord()
b2.print_coord()
b3.print_coord()
产生:Height: 1
Width: 2
X: 1
Y: 2
Height: 3
Width: 4
X: 3
Y: 4
Height: 5
Width: 6
X: 5
Y: 6