How do I make a Class extend Observable when it has extended another class too?

前端 未结 6 1774
野性不改
野性不改 2020-12-13 07:48

I am learning Java and I want to make my class into an observable class.

However I already have it extending another class.

What should I do?

6条回答
  •  自闭症患者
    2020-12-13 08:07

    Java doesn't allow multiple inheritance, so there is no direct way to do it. You should consider using a delegate pattern having your main object that delegates his observer behaviour to an another object..

    class YourObject extends ItsAncestorClass
    {
          private Observer includedObserver;
    
          public Observer getHisObserver(..)
    }
    

    Another approach would be turning the object from which your class is extending to an interface, then you'll be allowed to extend from Observer.

提交回复
热议问题