需要c++到Java翻译的帮助

Need help with C++ to Java translation

本文关键字:帮助 翻译 Java c++ 需要      更新时间:2023-10-16

下面是c++代码

public class First {
 public: 
     virtual int firstmethod(int a, int b) = 0;
     virtual int secondmethod(int a, int b, int c) = 0;
}
public class Second : public First {
 public:
    int firstmethod(int a, int b) {
        int result = a * b;
        return result + 3;
    }
}

这是目前为止我对Java的描述

public class First {
  static in firstmethod(int a, int b) = 0;
  static int secondmethod(int a, int b, int c) = 0;
}

public class Second extends First
{
   static int firstmethod(int a, int b)
   {
     int result = a * b;
     return result + 3;
   }
}

这样对吗?编辑我编辑了这个问题,使它更清晰,更容易理解

应该是这样的:

public class Whatever {
  public int mymethod(int one, int two) { return 0; }
  public int myothermethod(int one, int two, int three) { return 0; }
}

public class However extends Whatever
{
   @Override // optional annotation
   public int mymethod(int one, int two)
   {
     int answer = one * two;
     return answer + 3;
   }
}

然后你可以实例化Whatever。为了防止实例化Whatever,要么将其标记为abstract,要么将其标记为interface。这完全取决于您希望您的类如何继承Whatever。既然不能多重继承,请明智地选择。

public interface Whatever {
   public int mymethod(int one, int two);
   public int myothermethod(int one, int two, int three);
}
public class However implements Whatever
{
   public int mymethod(int one, int two)
   {
     int answer = one * two;
     return answer + 3;
   }
   public int myothermethod(int one, int two, int three) {
     return 0;
   }
}

public abstract class Whatever {
   public abstract int mymethod(int one, int two);
   public abstract int myothermethod(int one, int two, int three);
}
public class However extends Whatever
{
   public int mymethod(int one, int two)
   {
     int answer = one * two;
     return answer + 3;
   }
   public int myothermethod(int one, int two, int three) {
     return 0;
   }
}

** EDIT **

从注释中得到一些启示后,您的c++到Java等效实际上是第三个结构,因为您在c++代码中使用virtual类方法。

不,一点也不。静态方法从来不是虚拟的,Java对纯虚拟方法不使用"=0"(它使用"abstract"关键字)。具有抽象方法的类本身必须标记为抽象。此外,Java方法在默认情况下不是公共的——每个方法必须单独标记为"public"。

我的翻译是:

public abstract class Whatever {
    public abstract int mymethod(int one, int two);
    public abstract int myothermethod(int one, int two, int three);
}
public class However extends Whatever {
    @Override
    public int mymethod(int one, int two) {
        int answer = one * two;
        return answer + 3;
    }
    @Override
    public int myothermethod(int one, int two, int three) {
        return ...;
    }
}

我也喜欢Yanick关于使用接口的回答;这是一个更好的方法。我保留我的答案,因为使用@Override,这是有用的Java代码。

我想说你正在寻找的java翻译:

public interface Whatever {
     public static int myMethod(int one, int two);
     public static int myOtherMethod(int one, int two, int three);
}
public class However implements Whatever {
     public static int myMethod(int one, int two) {
          int answer = one * two;
          return answer + 3;
     }
     public static int myOtherMethod(int one, int two, int three) {
          int answer = one * two;
          return answer + 3;
     }
}

同样,为了清晰和惯例,我将谨慎地将变量命名为"one"或"two",因为这可能会导致混淆。