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

java - unable show chart in xls document using jasper reports -

java.lang.RuntimeException: Could not generate dummy secret at sun.security.ssl.RSAClientKeyExchange.<init> -

javascript - How to change image src on success AJAX -