课程: Python: Design Patterns (2021)
今天就学习课程吧!
今天就开通帐号,24,700 门业界名师课程任您挑!
Composite example
- Let's start by defining our abstract class, which is component. Nothing too much is done in the Component class definition, except for defining our interface method, component underscore function. Let's move on to the child class definition. Since this one inherits from the Component class, we'll have to say its parent is Component. Right here. We'll have an attribute to store the name of the child. Type self dot name space assignment space args. The index is zero. What we're doing here is getting an argument from the instantiation process of the child class, whose first argument happens to be the one we are going to be using for setting the name attribute. The second method here, implements the abstract method component underscore function. Our implementation prints the name of the child. Let's move on to the Composite class that also inherits from the abstract class, Component. We also need another…