0

これはメソッドを呼び出す 1 つの方法だと言われています。

メソッドまたはプロパティの名前だけを記述すると、Java は次の規則に基づいて、名前の前に記述する意味を推測します。

  1. メソッドが静的でない場合、その名前で非静的メソッド/プロパティを見つけようとし、次に静的メソッド/プロパティを探します。
  2. メソッドが静的である場合、静的メソッド/プロパティのみを見つけようとします

誰かが私にこれの例を教えてもらえますか? メソッドを見つける前にメソッドが静的かどうかをどのように知ることができるので、それが何を意味するのか理解できませんが、非静的か静的かに基づいてメソッドを見つけますか? それとも、彼らが言及している2つの異なる方法か何かがありますか?

4

1 に答える 1

2

以下は、メソッド c、d、および e で何が起こるかについての適切なコメントを含む例です。

class A {
    // methods to be looked up
    // a static method
    static void a() {};
    // non-static method
    void b() {};


    static void c() {
         // valid reference to another static method
         a();        
    }

    static void d() {
         // This would fail to compile as d is a static method 
         // but b is a non-static
         b();        
    }

    // non-static method would compile fine
    void e() {
       a(); // non-static method can find a static method 
       b(); // non-static method can find another non-static method
    }

}
于 2012-12-01T19:39:12.740 に答える