Advertisement
Lyuben_Andreev

Task3Polymorphism

Aug 21st, 2024
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.77 KB | Source Code | 0 0
  1. from shared import Shape
  2.  
  3. class Square(Shape):
  4.     def __init__(self, x, y, side_length):
  5.         self.x = x
  6.         self.y = y
  7.         self.side_length = side_length
  8.  
  9.     def area(self):
  10.         return self.side_length ** 2
  11.  
  12.     def to_dict(self):
  13.         return {
  14.             "type": "Square",
  15.             "attributes": {
  16.                 "x": self.x,
  17.                 "y": self.y,
  18.                 "side_length": self.side_length
  19.             }
  20.         }
  21.  
  22.     def __str__(self):
  23.         return f"Square at ({self.x}, {self.y}) with side length {self.side_length}"
  24.  
  25. class Circle(Shape):
  26.     def __init__(self, x, y, radius):
  27.         self.x = x
  28.         self.y = y
  29.         self.radius = radius
  30.  
  31.     def area(self):
  32.         import math
  33.         return math.pi * (self.radius ** 2)
  34.  
  35.     def to_dict(self):
  36.         return {
  37.             "type": "Circle",
  38.             "attributes": {
  39.                 "x": self.x,
  40.                 "y": self.y,
  41.                 "radius": self.radius
  42.             }
  43.         }
  44.  
  45.     def __str__(self):
  46.         return f"Circle at ({self.x}, {self.y}) with radius {self.radius}"
  47.  
  48. class Ellipse(Shape):
  49.     def __init__(self, x, y, width, height):
  50.         self.x = x
  51.         self.y = y
  52.         self.width = width
  53.         self.height = height
  54.  
  55.     def area(self):
  56.         import math
  57.         return math.pi * (self.width / 2) * (self.height / 2)
  58.  
  59.     def to_dict(self):
  60.         return {
  61.             "type": "Ellipse",
  62.             "attributes": {
  63.                 "x": self.x,
  64.                 "y": self.y,
  65.                 "width": self.width,
  66.                 "height": self.height
  67.             }
  68.         }
  69.  
  70.     def __str__(self):
  71.         return f"Ellipse at ({self.x}, {self.y}) with width {self.width} and height {self.height}"
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement