2016-04-21 118 views
-3

在HelloWorldAnonymousClasses示例程序(from here):什么在HelloWorldAnonymousClasses中调用了greet()方法?

/* 
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 
* 
* Redistribution and use in source and binary forms, with or without 
* modification, are permitted provided that the following conditions 
* are met: 
* 
* - Redistributions of source code must retain the above copyright 
*  notice, this list of conditions and the following disclaimer. 
* 
* - Redistributions in binary form must reproduce the above copyright 
*  notice, this list of conditions and the following disclaimer in the 
*  documentation and/or other materials provided with the distribution. 
* 
* - Neither the name of Oracle or the names of its 
*  contributors may be used to endorse or promote products derived 
*  from this software without specific prior written permission. 
* 
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
*/ 

public class HelloWorldAnonymousClasses { 

    interface HelloWorld { 
     public void greet(); 
     public void greetSomeone(String someone); 
    } 

    public void sayHello() { 

     class EnglishGreeting implements HelloWorld { 
      String name = "world"; 
      public void greet() { 
       greetSomeone("world"); 
      } 
      public void greetSomeone(String someone) { 
       name = someone; 
       System.out.println("Hello " + name); 
      } 
     } 

     HelloWorld englishGreeting = new EnglishGreeting(); 

     HelloWorld frenchGreeting = new HelloWorld() { 
      String name = "tout le monde"; 
      public void greet() { 
       greetSomeone("tout le monde"); 
      } 
      public void greetSomeone(String someone) { 
       name = someone; 
       System.out.println("Salut " + name); 
      } 
     }; 

     HelloWorld spanishGreeting = new HelloWorld() { 
      String name = "mundo"; 
      public void greet() { 
       greetSomeone("mundo"); 
      } 
      public void greetSomeone(String someone) { 
       name = someone; 
       System.out.println("Hola, " + name); 
      } 
     }; 
     englishGreeting.greet(); 
     frenchGreeting.greetSomeone("Fred"); 
     spanishGreeting.greet(); 
    } 

    public static void main(String... args) { 
     HelloWorldAnonymousClasses myApp = 
      new HelloWorldAnonymousClasses(); 
     myApp.sayHello(); 
    }    
} 

什么调用englishGreeting对象的greet()方法?我很困惑..

编辑

感谢您的回复。我应该已经发现了!我试图解决另一个查询,这就是:

在示例程序上 https://gist.github.com/bernii/5697073

在代码

this.wait.until(new ExpectedCondition<Boolean>() { 
      public Boolean apply(WebDriver webDriver) { 
       System.out.println("Searching ..."); 
       return webDriver.findElement(By.id("resultStats")) != null; 
      } 
}); 

什么叫在匿名内部类的应用()方法?

编辑

我现在问我的第二个查询中的新问题。 的适用于─方法从

WebDriverWait.until() 
+4

提示:在该页面上找到'englishGreeting.greet()'。 –

+0

它在主要方法之前就是正确的:/ –

回答

2

称为在代码的部分中示出:

2

中的greet()的englishGreeting对象的方法

englishGreeting.greet(); 

更新SpanishGreeting类之后称为下面创建三个HelloWorld接口实例:

HelloWorld englishGreeting = new EnglishGreeting();   
    HelloWorld frenchGreeting = new HelloWorld() { 
     //... 
    };   
    HelloWorld spanishGreeting = new HelloWorld() { 
     // ... 
    }; 

这里有些方法是呼吁那些实例:

englishGreeting.greet(); 
    frenchGreeting.greetSomeone("Fred"); 
    spanishGreeting.greet(); 

法语和西班牙语版本创建使用匿名内部类。英文版本由本地类声明(英文版)创建。注意:你没有经常看到本地类的声明,这是一个解释的例子。

相关问题