小弟初学JAVA,想不明白一个问题,为什么在类中调用函数一定要静态地调用?
代码如下:
为什么不可以像C++里面那样直接调用呢?
望大侠不甚赐教!public class ghost {
public static void ghost()
{
System.out.println("函数调用成功!!!");
}
public static void main(String args[] )
{
System.out.println("ghost is a big worry");
for(int a=0;a<10;a++)
{
ghost();
}
}}
代码如下:
为什么不可以像C++里面那样直接调用呢?
望大侠不甚赐教!public class ghost {
public static void ghost()
{
System.out.println("函数调用成功!!!");
}
public static void main(String args[] )
{
System.out.println("ghost is a big worry");
for(int a=0;a<10;a++)
{
ghost();
}
}}
因为main()是static,在main()中调用ghost(),所以ghost()需要是静态的!
另外想问一下什么叫"像C++里直接调用"呢
china8848(永远在一起)你好!
请问我还是不是很理解,你能否再写详细一点,谢谢先!
System.out.println("函数调用成功!!!");
}public static void main(String args[] ) {
System.out.println("ghost is a big worry");for(int a=0;a<10;a++) {
new Ghost().shout();
}
}
}
比如调用System.out.println()
还有Math.cos()等函数的调用。
静态方法可以直接用类然后.方法调用。
以后多编程就自然清楚了。