Created
May 4, 2025 19:50
-
-
Save CoffeeDeath/a26c046a61387b2d202fcfe335860463 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Rectangle: # изменяемый прямоугольник | |
def __init__(self, height=0, width=0): | |
# Инварианты: высота и ширина неотрицательны | |
assert height >= 0 and width >= 0 | |
self._height = height | |
self._width = width | |
@property | |
def height(self): | |
return self._height | |
@height.setter | |
def height(self, value): | |
# Предусловие | |
assert value >= 0 | |
self._height = value | |
# Постусловие | |
assert self._height == value | |
@property | |
def width(self): | |
return self._width | |
@width.setter | |
def width(self, value): | |
# Предусловие | |
assert value >= 0 | |
self._width = value | |
# Постусловие | |
assert self._width == value | |
@property | |
def area(self): | |
# Инвариант | |
assert self.height * self.width >= 0 | |
return self.height * self.width | |
@property | |
def perimeter(self): | |
# Инвариант | |
assert 2 * (self.height + self.width) >= 0 | |
return 2 * (self.height + self.width) | |
class Square(Rectangle): # изменяемый квадрат | |
@property | |
def height(self): | |
return super().height | |
@height.setter | |
def height(self, value): | |
# Предусловие | |
assert value >= 0 | |
super(Square, self.__class__).height.fset(self, value) | |
super(Square, self.__class__).width.fset(self, value) | |
# Постусловие: стороны равны value | |
assert self._height == value and self._width == value | |
@property | |
def width(self): | |
return super().width | |
@width.setter | |
def width(self, value): | |
# Предусловие | |
assert value >= 0 | |
super(Square, self.__class__).height.fset(self, value) | |
super(Square, self.__class__).width.fset(self, value) | |
# Постусловие: стороны равны value | |
assert self._height == value and self._width == value | |
def set_height_and_width(r: Rectangle): | |
r.height = 10 | |
r.width = 5 | |
assert r.area == 50 #неверно для квадрата => принцип лисков нарушен | |
r = Square() | |
set_height_and_width(r) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Rectangle: # неизменяемый прямоугольник | |
def __init__(self, height: int, width: int): | |
assert height >= 0 and width >= 0 | |
self._height = height | |
self._width = width | |
@property | |
def height(self): | |
return self._height | |
@property | |
def width(self): | |
return self._width | |
@property | |
def area(self): | |
return self.height * self.width | |
@property | |
def perimeter(self): | |
return 2 * (self.height + self.width) | |
def __repr__(self): | |
return f"Rectangle({self.height}, {self.width})" | |
class Rectangle: | |
def __init__(self, height: int, width: int): | |
assert height >= 0 and width >= 0, "Height and width must be non-negative" | |
self._height = height | |
self._width = width | |
@property | |
def height(self): | |
return self._height | |
@property | |
def width(self): | |
return self._width | |
@property | |
def area(self): | |
return self.height * self.width | |
@property | |
def perimeter(self): | |
return 2 * (self.height + self.width) | |
def __repr__(self): | |
return f"Rectangle(height={self.height}, width={self.width})" | |
class Square(Rectangle): # неизменяемый квадрат | |
def __init__(self, side: int): | |
assert side >= 0 | |
super().__init__(side, side) | |
def printRectangle(r:Rectangle): # квадрат не изменяем следовательно LSP соблюден тк функции не могут менять обьект | |
print(r.width) | |
print(r.height) | |
print(r.area) | |
print(r.perimeter) | |
a = Rectangle(10, 5) | |
b = Square(5) | |
check(a) | |
check(b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment