好吧,所以我必须创建两个类(两个不同的脚本),都称为Block,它存储有关矩形块的位置和大小的信息。版本1应具有用于存储块中心坐标的属性(或者作为单独的x坐标或y坐标,或者作为一对数字)以及块的宽度和高度。版本2应该具有用于存储左下角(“SW”角)的坐标和右上角(“NE”角)的坐标的属性。一组函数的可选参数 - python
所以我知道如何为每个构造函数单独设置构造函数,但对于这个赋值,两个版本都应该有一个构造函数,该构造函数将中心坐标与宽度和高度一起作为浮点数字)或表示块的任何两个对角的两对坐标。这是我迄今为止的尝试:
class Block:
"""Stores information about the position and size of a rectangular block.
Attributes: x-coordinate (int), y-coordinate (int), width (int), height (int) OR northeast corner (int) and southwest corner (int)"""
def __init__(self, center = '', width = '', height = '', SW = '', NE = ''):
"""A constructor that assigns attributes to the proper variables
Block, tuple, tuple -> None"""
self.center = center
self.width = width
self.height = height
self.SW = SW
self.NE = NE
但我敢肯定,实际上并不按照我想要的方式工作。基本上我需要能够输入一组变量作为中心,宽度和高度,或者我需要输入两个角落。有没有办法做到这一点?
'B =块(SW = 3,宽度= 1)'... ..运作良好....这是问题所在? –
你想如何使用构造函数?请举一些例子。你是否希望所有的字段都相应地设置或者保持为无? –