有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java这两个定义在功能上有什么不同?

// 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;
    }

}

vs

// 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;
    }
}

我试图理解这两种情况下PingPingActor之间的关系。在我看来,两者都在做同样的事情,但在一种情况下PingPong.Ping是在PingActor内部定义的,而在另一种情况下PingPong是它之外的另一个类。显然,它们不一样,因为第二个示例似乎可以编译,但第一个示例不能

编译器错误为- Inferred type 'M' for type parameter 'M' is not within its bound; should extend 'com.lightbend.akka.sample.PingPong.PingReceive<PingPong.Ping> createReceive()


共 (1) 个答案

  1. # 1 楼答案

    在PingPongMessage中不能有公共类“PingPong”。JAVA 他们不匹配