你能在main方法中写一个方法吗?例如,我找到了这段代码:
public class TestMax {
public static void main(String[] args) {
int i = 5;
int j = 2;
int k = max(i, j);
System.out.println("The maximum between is " + k);
}
public static int max(int num1, int num2) {
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
方法max可以在main方法中编码吗?
我有一个ExtA类,它包含一个过滤函数来过滤ArrayList:
public ExtA<T> filt(...)
{
//code
}
Run Code Online (Sandbox Code Playgroud)
当我编译它是给我错误:找不到符号 - 类T.这是为什么?
我有这种方法(使用泛型):
public class ExtA<T>Ex<T> extends ArrayList
{
public<T> ExtA<T>Ex<T> filt(Function<T, Boolean> a...)
{
//code
}
}
Run Code Online (Sandbox Code Playgroud)
给我一个"不兼容的类型错误".
我试图用C++编写一个构造函数(我是新的).
我的尝试:
class Tree
{
private:
int leaf;
public:
Tree(int leaf); //constructor
};
Tree::Tree(int leaf) //constructor
{
strcpy(this->leaf, leaf);
}
Run Code Online (Sandbox Code Playgroud)
这是怎么做的正确方法?因为我发现许多不同版本的srcpy,没有等等.
我总是使用C,当你有3个文件时,例如:tree.c,tree.h和main.c,在main.c中我总是包含tree.c和tree.c我总是包含tree.h. 现在在C++中这是一样的吗?因为我发现了一些教程,在tree.cpp中它们包含tree.h,而在main.cpp中它们也包括tree.h(而不是像C中那样的tree.cpp).
对不起,如果它令人困惑
我想了解C++.任何人都可以解释这段代码的确切含义,我知道它是Java中的某种类型的setter和getter,但我不确定.
Comm::Comm(const char* id)
{
strcpy(this->id, id);
}
char* Comm::getId()
{
return id;
}
Run Code Online (Sandbox Code Playgroud)