【问题标题】:Is it possible to subclass a Java object in the constructor?是否可以在构造函数中对 Java 对象进行子类化?
【发布时间】:2011-07-07 02:50:46
【问题描述】:

是否可以在构造函数中对 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();
  }


}

【问题讨论】:

  • 如果您将某些内容声明为受保护,您可能会忘记@Override?​​span>
  • 我是 Java 新手,正在学习关于子类、匿名类等的 Java 视图,@override 已经出现在 Netbeans 中,所以我现在正在查找它。

标签: java constructor selenium-webdriver subclassing


【解决方案1】:

您提供的代码并未修改原始类,而是创建了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

【讨论】:

    【解决方案2】:

    如果没有HtmlUnitDriver 类的Javadoc,很难对这个问题给出明确的答案。如果HtmlUnitDriver 是抽象类或接口,那么您问题中的示例代码称为匿名类。否则,代码只是简单地覆盖了类的方法。

    【讨论】:

      【解决方案3】:

      在 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;
        }
      };
      

      添加覆盖后,我在 creds 初始化中缺少 new,这是一个 Java 新手错误。 感谢您的帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-30
        • 1970-01-01
        • 1970-01-01
        • 2020-09-09
        • 1970-01-01
        • 2017-09-05
        相关资源
        最近更新 更多