Does java have an equivalent to python __call__? -


just started learning python, i've come across __call__ method. understand when __init__ gets called , when __call__ gets called running simple example:

class constructor:     def __init__(self, a):         print "__init__ called"      def __call__(self):         print "__call__ called"      def dummy(self):         print "a dummy method called"  print "creating x obj" x = constructor(1) print "calling x object" x() 

console output:

creating x obj __init__ called calling x object __call__ called 

my questions are:

  1. i come java background, know __init__ maps java constructor, java have equivalent __call__ me understand better?
  2. can give me simple example of when __call__ can useful?

__call__ equates overloading () operator on class. possible in c++ not java. allows 1 have function objects (sometimes called functors).

in python these termed callable objects.

in simple terms, allows treat object function.

the actual use such concepts beyond scope of answer there plenty of information around internet. example wikipedia.


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -