需要c++到Java翻译的帮助
Need help with C++ to Java translation
下面是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",因为这可能会导致混淆。
相关文章:
- 需要帮助设置在C++中使用的Potrace
- 不同翻译单元中不可重载的非内联函数定义
- 在指针的帮助下,文本文件中单词的频率
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 如何在Qbutton的帮助下更改Q对话框的宽度
- 需要帮助将结构数组传递给函数
- C++需要帮助从用户那里获得一个整数,并确保它在另外两个整数之间
- 需要将一行代码从C++ Qt翻译成PyQt
- 为什么未命名的结构内联变量在每个翻译单元中没有相同的地址?
- 需要帮助在 c++ 中将字符串转换为字符 ----错误 "const char *" 类型的值不能用于初始化 "char" 类型的实体
- 有人可以帮助我处理正则表达式吗?
- C++调用具有 *this 属性的单个帮助程序函数
- C++:需要帮助了解运算符重载错误
- 需要以下代码的帮助,下面的代码有什么问题
- CS1 项目帮助C++
- 用于检查值是否为其任何参数的帮助程序函数
- 需要有关此 if 语句的帮助
- 类型限定宏帮助程序
- CoreCLR 中的检测探查器 - 将帮助程序程序集加载到 dotnet 进程的方法
- 需要c++到Java翻译的帮助