Write a subclass called Rcircle of the superclass Circle.
Circle__init__(self, r).Rcircle subclass must reimplement the radius function. It does not make sense for Rcircle to inherit the radius method from Circle since an instance of Rcircle doesn't know anything about the coordinates of the circle.__eq__ special method to compare two circles.Demo your class.
Your Circle class from last time should have looked something like this:
In [19]:
import numpy as np
class Circle:
def __init__(self, x, y):
self.x = x
self.y = y
def radius(self):
self.R = np.sqrt(self.x * self.x + self.y * self.y)
return self.R
def area(self):
try:
self.A = np.pi * self.R* self.R
return self.A
except AttributeError:
r = np.sqrt(self.x * self.x + self.y * self.y)
self.A = np.pi * r * r
return self.A
def circum(self):
try:
self.C = 2.0 * np.pi * self.R
return self.C
except AttributeError:
r = np.sqrt(self.x * self.x + self.y * self.y)
self.C = 2.0 * np.pi * r
return self.C
In [25]:
class Rcircle(Circle):
def __init__(self,r):
if r<0:
raise ValueError ("r<0!")
self.R=r
def radius(self):
return self.R
def __eq__(self,other):
return self.R==other.R
In [26]:
r_c=Rcircle(-1)
print(r_c.circum())
In [ ]: