Overriding a method in an instantiated Java object

前端 未结 9 1409
陌清茗
陌清茗 2021-02-05 01:55

I would like to override a method in an object that\'s handed to me by a factory that I have little control over.

My specific problem is that I want to override the

相关标签:
9条回答
  • 2021-02-05 02:20

    Since Java uses class-based OO, this is impossible. What you can do is use the decorator pattern, i.e. write a wrapper for the object that returns the wrapped streams.

    0 讨论(0)
  • 2021-02-05 02:24

    two options:

    1. easy : if Foo were you implemetn an interface you can use a Dynamic proxy to add new functionality.
    2. more work: what you have is an "around" advice of AOP - you can use any of the existing AOP tools to make that possible. Spring Framework can do it for you, if you are using it already.
    0 讨论(0)
  • 2021-02-05 02:26

    I'm not sure if this is possible. Have you considered creating your own class, having the object returned by the factory as a member, and then writing the doBar() method for that class.

    0 讨论(0)
提交回复
热议问题