是否可以在构造函数中子类化Java对象?

时间:2011-07-07 02:50:46

标签: java constructor selenium-webdriver subclassing

是否可以在构造函数中继承Java对象?

我是一个Java新手,在这里的文章http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions中试用了Selenium。有一个关于如何修改HtmlUnitDriver驱动程序对象以支持身份验证的说明,我在这里重复了一些演示代码。

WebDriver driver = new HtmlUnitDriver() {
  protected WebClient modifyWebClient(WebClient client) {
    // This class ships with HtmlUnit itself
    DefaultCredentialsProvider creds = DefaultCredentialsProvider();

    // Set some example credentials
    creds.addCredentials("username", "password");

    // And now add the provider to the webClient instance
    client.setCredentialsProvider(creds);

    return client;
  }
};

代码是一个进入子类定义的例子,还是一个'内联'的修改?我假设它是可能的,但是当我将它复制到IDE中时,我得到语法错误,表明某些属性未定义。

在了解了有关Java,匿名类和覆盖的更多信息之后,这是我当前的代码。 但我在Netbeans中的DefaultCredentialsProvider上遇到语法错误,我不确定是否是由于缺少必需的类,或者是否需要进行更多更改。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package seleniumtest01;

/**
 *
 * @author richard
 */
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import com.gargoylesoftware.htmlunit.DefaultCredentialsProvider;
import com.gargoylesoftware.htmlunit.WebClient;
//import org.openqa.selenium.htmlunit.ChromeDriver;

public class Main {

  public static void main(String[] args) {
    // Create a new instance of the Firefox driver
    // Notice that the remainder of the code relies on the interface,
    // not the implementation.
    testBasicAuth();
    System.exit(0);
  }

  public static void testBasicAuth() {
    // Create a new instance of the Firefox driver
    // Notice that the remainder of the code relies on the interface,
    // not the implementation.
    //WebDriver driver = new FirefoxDriver();
    WebDriver driver = new HtmlUnitDriver() {

      @Override
      protected WebClient modifyWebClient(WebClient client) {
        // This class ships with HtmlUnit itself
        DefaultCredentialsProvider creds = DefaultCredentialsProvider();

        // Set some example credentials
        creds.addCredentials("username", "password");

        // And now add the provider to the webClient instance
        client.setCredentialsProvider(creds);

        return client;
      }
    };
    driver.get("http://user:selenium@192.168.1.2/");
    new WebDriverWait(driver, 10);

    WebElement element = driver.findElement(By.xpath("//a[text()='Connection']"));
    element.click();
    //element = driver.findElement(By.xpath("//a[text()='Admin Login']"));
    element = driver.findElement(By.xpath("//a[contains(@href, 'admin/connection')]"));//[contains(@href,'#id1')]
    element.click();
    element = driver.findElement(By.xpath("//a[text()='Connection 1']"));
    element.click();
    element = driver.findElement(By.name("field_one"));
    element.clear();
    element.sendKeys("sample text");
    //driver.findElement(By. id("submit")).click();
    element.submit();

    new WebDriverWait(driver, 10);
    driver.quit();
  }


}

3 个答案:

答案 0 :(得分:2)

您提供的代码不是修改原始类,而是创建HtmlUnitDriver的匿名子类。

例如:

class A {
  void sayHello() { System.out.println("Hello!"); }
}

class Main {
  public static void main(String[] args) {
    A a = new A() {
      @Override void sayHello() { System.out.println("Good bye"); }
    }

    a.sayHello();
  }
}

这将打印Good bye。局部变量a持有的实例的类型是一个匿名类,由编译器自动生成。该类的名称将类似于Main$0

答案 1 :(得分:0)

如果没有班级HtmlUnitDriver的Javadoc,很难给出明确的答案。如果HtmlUnitDriver是抽象类或接口,则问题中的示例代码称为匿名类。否则,代码只是覆盖了类的方法。

答案 2 :(得分:0)

在sqa.stackexchange.com上发布问题后,我得到的是WebDriver的构造函数应该是:

WebDriver driver = new HtmlUnitDriver() {

  @Override
  protected WebClient modifyWebClient(WebClient client) {
    // This class ships with HtmlUnit itself
    DefaultCredentialsProvider creds = new DefaultCredentialsProvider();

    // Set some example credentials
    creds.addCredentials("user", "selenium");

    // And now add the provider to the webClient instance
    client.setCredentialsProvider(creds);

    return client;
  }
};

添加覆盖后,我错过了new到creds初始化,这是一个Java新手错误。 谢谢你的帮助