我对Selenium WebDriver完全陌生。谁能告诉我这两条线有什么区别吗?
WebDriver driver = new FirefoxDriver();和
FirefoxDriver d = new FirefoxDriver();两者都启动Firefox浏览器。那么为什么我们总是写第一行而不是第二行呢?
发布于 2014-07-13 04:36:47
WebDriver是一个接口。
FirefoxDriver是实现。
为了更好地理解,请阅读Java接口上的文档。
发布于 2014-07-13 11:35:48
这就是所谓的“Java中的静态和动态绑定”。
你可以用上面的话搜索它,你会得到很多网站。
简单地告诉你:
class vehicle
{
public void print(String str)
{ System.out.println("I am string "+str);}
public void print(Integer int)
{System.out.println("I am integer:"+int);}
public static void main(String[] args)
{
vehicle obj=new vehicle();
obj.print("Hello"); //Then it is clear that it will call first print method i.e String
} //This is method overloading.
}这是在编译时决定的。所以静态绑定。
另一种情况是:
class vehicle
{
void start(){System.out.println("Vehicle started");}
}
class car extends vehicle
{
void start(){System.out.println("Car started");}
}
public static void main(String[] args)
{
vehicle obj=new car();
obj.start(); //Here it prints Car's start method and is decided at run time so dynamic binding
}
}
} //This is method overriding根据你的问题:
WebDriver driver=new FirefoxDriver() //This is dynamic binding
FirefoxDriver driver=new FirefoxDriver() //Kind of static bindinghttps://stackoverflow.com/questions/24719556
复制相似问题