简体   繁体   English

WebDriver driver = new FirefoxDriver()是编译时绑定还是运行时绑定?

[英]WebDriver driver=new FirefoxDriver() is this compile-time or runtime binding?

It looks like I got a bit confused in my core Java concepts .Please have a look below . 看起来我对Java的核心概念有些困惑。请在下面查看。

    class A{
       public void func1()
       {
           System.out.println("Hello A");
       }
    }
    class B extends A{
       public void func1(){
           System.out.println("Hello B");
       }
     }
   class C {
        public static void main( String args[]) {

           A myobj = new B();
           myobj.func1();
       }
}

As we know Webdriver is an interface & FirefoxDriver is the implementing class for Webdriver , so based on the above example is it right to assume that the statement : WebDriver driver=new FirefoxDriver() is an example of late binding. 我们知道Webdriver是接口,而FirefoxDriver是Webdriver的实现类,因此基于上述示例,可以正确地假设以下语句: WebDriver driver=new FirefoxDriver()是后期绑定的示例。

WebDriver driver=new FirefoxDriver() is this compile-time or runtime binding? WebDriver driver=new FirefoxDriver()是编译时绑定还是运行时绑定?

That's compile-time binding to the class called FirefoxDriver . 那是在编译时绑定到名为FirefoxDriver的类。 It will use whatever FirefoxDriver class is in the classpath. 它将使用classpath中的任何FirefoxDriver类。 This is just about as compile-time as Java gets, as Java works from the classpath at compile-time and also at runtime. 这与Java获得的编译时间差不多,因为Java在编译时和运行时都从类路径进行工作。

Here's an example of runtime binding: 这是运行时绑定的示例:

String driverName = /*...get the name from somewhere, like a properties file...*/;
WebDriver driver = Class.forName(driverName).newInstance();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM