- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
class Container:
def __init__(self, liquid):
self.liquid = liquid
def look_inside(self):
return f"{self.liquid} in container"
@classmethod
def create_with(cls, liquid):
return cls(liquid)
class Bottle(Container):
def look_inside(self):
return f"bottle full of {self.liquid}"
class Glass(Container):
def look_inside(self):
return f"A glass of {self.liquid}"
for c in (c.create_with("beer") for c in [Glass, Bottle]):
print(c.look_inside())
DypHuu_niBEHb 23.10.2019 04:42 # 0
gem 23.10.2019 10:40 # 0
https://ideone.com/kvNwig
Вместо метода create_with можно вообще определить __toString, тогда в цикле достаточно будет echo Container::create_with($c, 'beer').
Elvenfighter 24.10.2019 12:01 # +2
guest8 24.10.2019 12:03 # −999
Elvenfighter 24.10.2019 12:46 # 0
---
Elvenfighter 24.10.2019 11:35 # 0
guest8 24.10.2019 12:00 # −999
Elvenfighter 24.10.2019 12:18 # 0