Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java/C中,声明的顺序重要吗? C++中,如果在调用方法之后声明它,则不能使用方法。_Java_C# - Fatal编程技术网

在Java/C中,声明的顺序重要吗? C++中,如果在调用方法之后声明它,则不能使用方法。

在Java/C中,声明的顺序重要吗? C++中,如果在调用方法之后声明它,则不能使用方法。,java,c#,Java,C#,这个顺序在Java或C#等其他语言中重要吗?我不确定C#,但在Java中你可以。不。在C#中不重要。无论是C#还是Java都不重要。在Java和C中都没有单独的方法声明 方法的声明与实现一起完成。您也不需要跟踪文件包含,这样只要类在同一名称空间中,它们就可以相互了解。变量应该可以在使用它的方法中访问。它是在使用之前还是之后声明并不重要。方法的声明顺序在C#或Java中并不重要。同样,在方法使用的变量之前还是之后声明方法也无关紧要 然而,变量的声明顺序可能很重要,当它们被初始化时,一个依赖于另一个


这个顺序在Java或C#等其他语言中重要吗?

我不确定C#,但在Java中你可以。

不。

在C#中不重要。

无论是C#还是Java都不重要。

在Java和C中都没有单独的方法声明


方法的声明与实现一起完成。您也不需要跟踪文件包含,这样只要类在同一名称空间中,它们就可以相互了解。

变量应该可以在使用它的方法中访问。它是在使用之前还是之后声明并不重要。

方法的声明顺序在C#或Java中并不重要。同样,在方法使用的变量之前还是之后声明方法也无关紧要

然而,变量的声明顺序可能很重要,当它们被初始化时,一个依赖于另一个。例如(C#):

但是:

Java阻止了这种情况,但很容易模仿:

public class Test
{
    static int y = getInitialValue();
    static int x = 5;

    public static void main(String args[])
    {
        System.out.println("x=" + x + " y=" + y);
    }

    static int getInitialValue()
    {
        return x;
    }
}
在C#中,当涉及到分部类时,情况变得更加混乱。初始化在C#中以文本顺序进行,但当有多个文件参与同一类时,该顺序没有完全定义


不用说,尽可能避免这种情况

对于Java,权威答案隐藏在Java语言规范的第1章(简介)(“JLS,第3版,可在线免费查看”):

声明顺序仅对局部变量、局部类以及类或接口中字段初始值设定项的顺序有效


不,编译器执行两次传递。

有一种棘手的情况,即从词汇上讲,要调用的函数可以在调用点之后声明,但不能在语义上声明。这是因为类被认为是在类成员函数体中完全定义的

$9.2.2-“一个班级被视为 完全定义的对象类型(3.9) (或完整类型)在 类说明符。在类中 成员规范,该类为 在功能上被认为是完整的 主体、默认参数和 构造函数初始化器 (包括嵌套的此类事物) 类),否则视为 在它自己的类中是不完整的 成员规范。”


在某些情况下,方法/构造函数的顺序在Java中确实很重要:

class Callee {
    private static void bar(int i) { } // compilation error if first
    public static void bar(String s) { } // ok if this method is first
}

class Caller {
    private void foo() { Callee.bar(bar()); }
    private <T> T bar() { return null; }
}
类被调用方{
私有静态无效条(inti){}//编译错误
公共静态无效条(字符串s){}//如果此方法是第一个,则确定
}
类调用者{
私有void foo(){Callee.bar(bar());}
私有T bar(){return null;}
}

这些答案相互矛盾,使事情难以理解。最好的做法是按照一般意义上的时间顺序声明方法、变量等,这样就不会出现混淆,这适用于所有编程语言。main始终是第一个,因此可以在开头或结尾,但仍然应该从main开始,并且当您在main中调用方法时,它应该是main之后的下一个方法,依此类推。至少对我来说,这是最有意义的,并且使代码最具可读性(注释也很有帮助,因为让我们面对它,代码真的是胡言乱语)。我不是编码专家,但我知道任何算法的最佳实践都是根据需要将其分解为尽可能多的简单步骤(请给出评论)。这没有道理:

   final List<int[]> intArrays = Arrays.stream(testarray).collect(Collectors.toList());
   final List<Integer> integers = intArrays.stream().flatMap(z -> 
                  Arrays.stream(z).boxed()).collect(Collectors.toList());

unless you add comments like:
final List<int[]> intArrays = Arrays.stream(testarray).collect(Collectors.toList());
// this makes a List (of ints) variable type that cannot be changed based on the stream of 
//(the testarray in this case) and uses the Collector method to add the ints
//to intArrays (our variable name)
final List<Integer> integers = intArrays.stream().flatMap(z -> 
                  Arrays.stream(z).boxed()).collect(Collectors.toList());
// I would have to look this up, because I honestly have no clue what it does exactly. 
final List intarray=Arrays.stream(testarray.collect)(Collectors.toList());
最终列表整数=intArrays.stream().flatMap(z->
Arrays.stream(z.boxed()).collect(Collectors.toList());
除非您添加以下注释:
最终列表intarray=Arrays.stream(testarray.collect)(Collectors.toList());
//这使得列表(ints)变量类型不能基于流更改
//(本例中为testarray)并使用收集器方法添加int
//要插入阵列(我们的变量名)
最终列表整数=intArrays.stream().flatMap(z->
Arrays.stream(z.boxed()).collect(Collectors.toList());
//我得查一下,因为我真的不知道它到底是干什么的。

就像我说的,代码基本上是胡言乱语。请帮助您自己和任何其他人查看您的代码并按逻辑顺序编写,无论您使用何种语言。(再说一次,请发表评论!稍后你会感谢我的。)

没有什么比简短和简洁更重要的了。不,那就足够了。这毕竟是一个肯定/否定的问题。句号前的空格是不必要的,而且会把答案弄得乱七八糟。我建议把它去掉,以防止人们被不必要的绒毛弄糊涂。我正在努力消除那个空白。微调后的最小响应长度为10(可能是有充分理由的)。我的答案实际上是否定的。我正在考虑发布一个StackOverflow问题,看看是否有人可以帮助我。如果没有解释,如果有人发布相反的意见,这个答案可能会变得毫无用处。例如,如果有人发表类似“是”的声明,这个回答如何帮助读者从两个相反的观点中选择?考虑到它的更好的形状,为了满足指南,请将单独的问题分别发布给java和c*。在它当前的形式中,您的查询对于堆栈溢出来说太广泛了。
struct A{
    void f(){g();}    // OK to call 'g' even if the compiler has not seen 'g' as yet
    void g(){};
};

int main(){
    A a;
    a.f();
}
class Callee {
    private static void bar(int i) { } // compilation error if first
    public static void bar(String s) { } // ok if this method is first
}

class Caller {
    private void foo() { Callee.bar(bar()); }
    private <T> T bar() { return null; }
}
   final List<int[]> intArrays = Arrays.stream(testarray).collect(Collectors.toList());
   final List<Integer> integers = intArrays.stream().flatMap(z -> 
                  Arrays.stream(z).boxed()).collect(Collectors.toList());

unless you add comments like:
final List<int[]> intArrays = Arrays.stream(testarray).collect(Collectors.toList());
// this makes a List (of ints) variable type that cannot be changed based on the stream of 
//(the testarray in this case) and uses the Collector method to add the ints
//to intArrays (our variable name)
final List<Integer> integers = intArrays.stream().flatMap(z -> 
                  Arrays.stream(z).boxed()).collect(Collectors.toList());
// I would have to look this up, because I honestly have no clue what it does exactly.