java - is it possible to call a class constructor and define it's body at the same time? -
this question has reply here:
class definition within method argument in java? 4 answersin below code snippet, know in statement counter c = new counter();
, new reference variable declared , memory location placed in new operator, while class constructor beingness called, don't understand rest of it!
is possible define constructor body there, { }
while it's beingness invoked! possible define method within constructor?
public class counter { private int counter = 0; public int count() { homecoming counter++; } }
and in class have:
counter c = new counter() { public int count() { super.count(); homecoming super.count(); } }
you're looking @ anonymous class derived counter
.
the body of derived class contained within braces. default constructor of counter
beingness called (you can't else anonymous class). count()
method beingness overridden.
the above more commonly performed interfaces, , provides java analogous closures (until java 8, @ least)
java oop constructor initialization
No comments:
Post a Comment