// in PingPongMessage.java
public class PingPong {
public static final class Ping { }
}
// in PingActor.java
public class PingActor extends AbstractBehavior<PingPong.Ping> {
public static Behavior<PingPong.Ping> create() {
return Behaviors.setup(context -> new PingActor(context));
}
private PingActor(ActorContext<PingPong.Ping> context){
super(context);
}
@Override
public Receive<PingPong.Ping> createReceive() {
return newReceiveBuilder().onMessage(PingPong.Ping.class, this::onPingMsg).build();
}
private Behavior<PingPong.Ping> onPingMsg() {
System.out.println("Ping!");
return this;
}
}
对比
// in PingActor.java
public class PingActor extends AbstractBehavior<PingActor.Ping>{
public static final class Ping {
}
public static Behavior<Ping> create() {
return Behaviors.setup(context -> new PingActor(context));
}
private PingActor(ActorContext<Ping> context){
super(context);
}
@Override
public Receive<Ping> createReceive() {
return newReceiveBuilder()
.onMessage(Ping.class, this::onPingMessage).build();
}
private Behavior<Ping> onPingMessage(Ping message){
System.out.println("Ping!");
return this;
}
}
我试图了解这两种情况下 Ping
和 PingActor
之间的关系。在我看来,两者都在做同样的事情,但在一种情况下,PingPong.Ping
是在 PingActor
内定义的,而在另一种情况下,PingPong
是它之外的另一个类。显然它们不一样,因为第二个例子似乎可以编译,但第一个没有。
编译器错误是 -
Inferred type 'M' for type parameter 'M' is not within its bound; should extend 'com.lightbend.akka.sample.PingPong.Ping
见于 Receive<PingPong.Ping> createReceive()
答案 0 :(得分:1)
在 PingPongMessage.java 中不能有公共类“PingPong” 他们不匹配