java - Override method from assigned class -


i want override method class that's assigned variable.
example:

inventory = new inventory( );  /* code here changes how inventory must behave or whatever */  inventory {     @override ... } 

is possible ???

maybe think of (instead of null-if implement default strategy make more clean):

public interface strategy {      public void dosomething();  }  public class inventory {      strategy strategy;      public inventory() {         // ...     }      public void dosomething() {         if (strategy == null) {             system.out.println("strategy empty");         } else {             strategy.dosomething();         }     }      public strategy getstrategy() {         return strategy;     }      public void setstrategy(strategy strategy) {         this.strategy = strategy;     }  } 

then this

    inventory inventory = new inventory();     inventory.dosomething();     inventory.setstrategy(new strategy() {          @override         public void dosomething() {             system.out.println("strategy different");         }      });     inventory.dosomething(); 

shows this:

strategy empty strategy different 

for more elaborated version can take @ strategy pattern.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -