对 super 关键字的困惑; Java 中实现的装饰器模式
上面的解释很好。
中给出的装饰模式 (DeP) 的实现有点困惑
上述链接代码的设计位于tinypic.com/view.php?pic=xnaqlt&s=3
我对“super.callTrailer();”感到困惑装饰器类 Header1、Header2、Footer1 和 Footer2 均派生自 TicketDecorator。
难道不应该只是“callTrailer();” ?由于“private Component myTrailer;”行,每个装饰器对象都会有自己的对下一个装饰器的引用。
注意:我不太精通 Java,并且是设计模式的初学者。
the above explanation is very nice.
However, I am slightly confused by the implementation of Decorator Pattern (DeP) as given in
The design for above linked code is given at tinypic.com/view.php?pic=xnaqlt&s=3
I am confused by "super.callTrailer();" in the decorator classes Header1, Header2, Footer1 and Footer2, all derived from TicketDecorator.
Shouldn't it be just "callTrailer();" ? As each decorator object would have its own reference to the next decorator due to the line "private Component myTrailer;".
Note: I am not very well versed in Java and a beginner in Design Patterns.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
他们都会给你相同的结果。由于callTrailer()仅存在于父类中,因此单独调用callTrailer()将自动调用TicketDecorator的callTrailer()。我猜他们把 super 放在那里的原因是为了明确它正在调用父级的方法。
They will both give you the same result. Since callTrailer() only exists in the parent class then calling callTrailer() by itself will automatically call the callTrailer() of the TicketDecorator. I guess the reason they put super in there is to make it explicit that it is calling the parent's method.