// allows passing in arguments to the MyActor constructor
ActorRef myActor = system.actorOf(new Props(new UntypedActorFactory()
{ //
public UntypedActor create() { //
return new MyActor("..."); // <- this part confuses me
} //
} //
), "myactor");
I am new to Java, going through the Akka documentation. and I find the above code confusing. Especially the code block below. How can a block of code be sent to the "new UntypedActorFactory()" constructor call. What is this type of constructor initialization called.
{
public UntypedActor create() {
return new MyActor("...");
}
The part that confused you creates an anonymous class, then instantiates it, and passes the newly created instance to a method as an argument. The block of code you are referring to is the body of anonymous class that is derived from UntypedActorFactory
For instance, if you have an interface:
interface SomeInterface {
void someMethod();
}
You can create an anonymous class that implements your interface like this (similar syntax is suitable for extending named classes):
SomeInterface instance = new SomeInterface() {
public void someMethod() { //
// implementation here // <- similar to the example,
} // this is the body of anonymous class
}; //
This is an anonymous class.
This is simply a new class inheriting from UntypedActorFactory with the declared method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.