0 java
如果我们声明的方法是静态的,则无需实例,我们可以在任何地方调用类身体内的方法.
如果我们不声明一个方法是静态的,那么可以实例化一个对象并调用该方法.
现在,如果我们不声明一个方法是静态的并且也没有实例化,我们可以在函数内调用一个函数吗?
编辑:
我现在明白了,我的预感是正确的.除非有静态或对象实例化,否则我们不能在函数内调用另一个函数.
但是在Java小程序中,我记得看到一个函数是从另一个函数调用的.
import javax.swing.*;
import java.awt.Container;
public class MethodCall extends JApplet
{
public void init()
{
String output = "";
JTextArea outputarea=new JTextArea(10,20);
Container c = getContentPane();
c.add(outputarea);
int result;
for(int x=1;x<=10;x++)
{
result = square(x);
output += "Square of " + x + " is " + result + "\n";
}//end of for loop
outputarea.setText(output);
}//end of init()
public int square(int y)
{
return y*y;
}//end of square()
}//end of class MethodCall
Run Code Online (Sandbox Code Playgroud)
见square()函数
Ami*_*hum 11
你的问题不是很清楚,但这里有一个简短的总结:
class A {
public static void foo() {
bar(); // illegal, no object
foo(); // legal, implicit
A.foo(); // legal, explicit
A a = new A();
a.bar(); // legal - we call a non-static function on an object
}
public void bar() {
bar(); // legal, implicit
this.bar(); // legal, explicit
foo(); // legal, implicit
A.foo(); // legal, explicit
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,在这种情况下,对函数内部函数的调用是无限递归的.
| 归档时间: |
|
| 查看次数: |
23065 次 |
| 最近记录: |