调用方法与类 [英] calling method Versus class.method
问题描述
我有一个类定义了两个方法。
public class Routines {
public static method1(){
/ *语句集* /
}
public static method2(){
/ *另一组语句* /
}
}
现在我需要从method2
以下哪种方法更好?
public static method2(){
method1();
}
或
$ b b
public static method2(){
Routines.method1();
}
同意现有的答案,这主要是一个风格问题,它是足够的风格问题,Eclipse和IntelliJ的代码批评会标记非静态引用静态方法在代码中,不使用 Classname.method()
style。
我通过使用类名来强调 intent 限定对静态目标的引用,
this
以限定对实例目标的引用,以及本地引用的裸名。现代IDE将对这些构造使用不同的突出显示,因此我认为这些天不太重要。我喜欢维护者(通常是我自己)来知道是什么意思,是的,我知道是一个 static
引用。 是的,它确实使稍微更冗长的代码,但我认为这是值得额外的字符。
I have a class with two methods defined in it.
public class Routines {
public static method1() {
/* set of statements */
}
public static method2() {
/* another set of statements.*/
}
}
Now I need to call method1() from method2()
Which one the following approaches is better? Or is this qualify as a question?
public static method2() {
method1();
}
OR
public static method2() {
Routines.method1();
}
While I agree with the existing answers that this is primarily a style issue, it is enough of a style issue that both Eclipse and IntelliJ's code critics will flag "non-static references to static methods" in code that does not use the Classname.method()
style.
I made it a habit to emphasize intent by using the classname to qualify references to static targets, this
to qualify references to instance targets, and bare names for local references. A modern IDE will use different highlighting for these constructs, so I suppose it is less important these days. I like for the maintainer (often myself) to know what was intended, that yes, I knew that was a static
reference.
Yeah, it does make for slightly more verbose code, but I think it is worth the extra characters.
这篇关于调用方法与类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!