擅长:python、mysql、java
<p>一种选择是在构造函数中独占关键字参数,并包含逻辑来确定应该使用什么:</p>
<pre><code>class Circle(object):
def __init__(self, points=(), radius=None, curves=()):
if radius and len(points) == 1:
center_point = points[0]
# Create from radius/center point
elif curves and len(curves) == 3:
# create from curves
elif points and len(points) == 3:
# create from points
else:
raise ValueError("Must provide a tuple of three points, a point and a radius, or a tuple of three curves)
</code></pre>
<p>您还可以使用classmethods为API用户简化操作:</p>
^{pr2}$
<p>用法:</p>
<pre><code>c = Circle.from_points(p1, p2, p3)
c = Circle.from_point_and_radius(p1, r)
c = Circle.from_curves(c1, c2, c3)
</code></pre>