课程: Python: Design Patterns (2021)

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Visitor example

Visitor example

- [Instructor] We'll start with defining the class that's being visited. The class is called house. One of the main features of the house class is this method that triggers the visiting operation. The method is called accept, and we'll be defining the method here. It gets this visitor instance and then invokes the visit method on a visitor instance. Type visitor. and visit is the name of the method, self. For the visitor to be able to visit, it needs a reference to an instance of the house class. That's why we type self here. We'll invoke this method called work underscore on hvac only when the visitor is hvac underscore specialist. We have another method called work underscore on underscore electricity, an electrician visitor calls. We already defined the work on hvac method, it displays this message worked on by so-and-so, which will be the name of the object. In this case, the hvac underscore specialist object. Will…

内容