2012-07-09 191 views
1

我正在清除我的Java概念。我对Java的了解远在开端,对我很友善。Java中的静态和非静态方法Intercall

我想了解静态方法和非静态方法intercalls。我知道 -

  1. 静态方法可以简单地调用另一个静态方法在同一类中的名称。
  2. 只有在创建类的实例后,静态方法才能调用同一类的另一个非staic方法。
  3. 非静态方法可以简单地通过classname.methodname调用同一类的另一个静态方法 - 不知道这是否正确?

我的问题是关于非静态方法调用另一个非staic相同类的方法。在类声明中,当我们声明所有方法时,我们可以从非静态类中调用另一个非静态方法吗?
请用实例说明。谢谢。

+1

你写了一个测试案例和尝试,也许? – 2012-07-09 21:07:54

+0

我认为如果你提供这个例子会更有帮助。用一两个方法提供一个简单的类声明。包括您询问的方法调用。然后我们可以说“是的,这是允许的”或者“不是这样,这是原因”。 – 2012-07-09 21:09:25

+0

当然你可以做到这一点,只需编写一个简单的类:2个静态方法,2个非静态方法,并尝试实现你所要求的,答案将足够清晰。 – Benjamin 2012-07-09 21:15:15

回答

0

不确定我是否正确理解问题,但非静态方法是在OO中设计类的标准方法。也许这个样本将有助于引发讨论:

public class MySampleClass{ 

    private void methodA(){ 
     System.out.println('a called'); 
    } 

    public void methodB(){ 
     this.methodA(); 
     staticMethod(this); 
    } 

    private static void staticMethod(MySampleClass inst){ 
     inst.methodA(); 
    } 
} 
6

你的#3,是正确的,你可以通过使用classname.methodname调用非静态方法静态方法。

而你的问题似乎是问你是否可以从其他非静态方法的类中调用非静态方法,这也是可能的(也是最常见的)。

例如:

public class Foo { 

public Foo() { 
    firstMethod(); 
    Foo.staticMethod(); //this is valid 
    staticMethod(); //this is also valid, you don't need to declare the class name because it's already in this class. If you were calling "staticMethod" from another class, you would have to prefix it with this class name, Foo 
} 

public void firstMethod() { 
    System.out.println("This is a non-static method being called from the constructor."); 
    secondMethod(); 
} 

public void secondMethod() { 
    System.out.println("This is another non-static method being called from a non-static method"); 

} 

public static void staticMethod() { 
    System.out.println("This is the static method, staticMethod"); 
} 
} 
0
public class TestClass{ 

    public static void testStatic(){ 
    System.out.println("test1"); 
    } 

    public void testNonStatic(){ 
    System.out.println("test2"); 
    } 

    public void test1(){ 
    // both is valid 
    testStatic(); 
    TestClass.testStatic(); 

    // this is valid, cause it can call the method of the same instance of that class 
    testNonStatic(); 
    this.testNonStatic(); 

    // this is not valid, cause without a concrete instance of a class you cannot call 
    // non static methods 
    TestClass.testNonStatic(); 
    } 

    public static void test2(){ 
    // again these are both correct 
    testStatic(); 
    TestClass.testStatic(); 

    // this doesn't work, cause you cannot call non static methods out of static methods 
    testNonStatic(); 
    this.testNonStatic(); 

    // this instead does work cause you have a concrete instance of the object 
    TestClass myTestClass = new TestClass(); 
    myTestClass.testNonStatic(); 

    // this still doesn't work 
    TestClass.testNonStatic(); 
    } 

} 
2

一种方法是,应该在第一方面在语义上结合至任一类或实例。

一个东西的列表有一个长度或大小,所以你问的特殊列表的大小。你需要该类的一个对象来呼叫.size()

一个典型的,众所周知的静态方法的例子是Integer.parseInt ("123");。您在那一刻没有Integer实例,但想要创建一个实例。

如果在所有的,我们将这个方法绑定到一个实例,我们将其绑定到一个String实例 - 这将使意义:

int a = "123".parseInt(); 

这本来是一个合理的选择,但它会意味着对于float,double,long,short,Boolean和可能每个具有对称“toString”方法的类都必须放入String中。这将意味着对String类的数以万计的扩展。

相反,String是最终的,所以放置这样一个方法的合理位置是目标类,如Integer,Float等。

+1

这是我清除所有疑惑的最好解释!谢谢朋友 – 2013-05-17 03:56:36

0

您可以使用显式引用您要调用该方法的对象someObject.method或不指定该对象someMethod()(在这种情况下,它将在与您相同的对象上调用),从非静态方法调用非静态方法正在调用当前的非静态方法)。

也许这将显示它更好

class Demo { 
    private String name; 

    public Demo(String n) { 
     name = n; 
    } 

    public String getName() {// non static method 
     return name; 
    } 

    public void test(Demo d) {// non-static method 
     System.out.println("this object name is: "+getName());// invoking method on this object 
     System.out.println("some other object name is: "+d.getName());// invoking method on some other object 
    } 
    //test 
    public static void main(String[] args) { 
     Demo d=new Demo("A"); 
     Demo d2=new Demo("B"); 
     d.test(d2); 
    } 
} 

输出:

this object name is: A 
some other object name is: B