gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:01:21 30 4
gpt4 key购买 nike

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

我是一名 Java 新手,在此处的一篇文章中尝试 Selenium http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions有一个关于如何修改 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();
}


}

最佳答案

您提供的代码不会修改原始类,而是创建 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();
}
}

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

关于java - 是否可以在构造函数中子类化 Java 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6605308/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com