如何从父类获取字符串到子类?请检查我的代码并告诉我该怎么做?我想获取从父类到子类的字符串。
public class ExtendExamle {
public static void main(String[] args) {
// TODO Auto-generated method stub
File file=new File("D:\\softs\\IEDriverServer_Win32_2.42.0\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
WebDriver driver = new InternetExplorerDriver();
driver.get("https://www.gmail.com");
}
public static class Test extends ExtendExample {
public static void main(String[] args) {
}
}
}
最佳答案
当一个类扩展另一个类时,子类会自动继承任何可见变量(未标记为 private
或没有访问修饰符的变量)。
class ParentClass {
protected String url = "www.stackoverflow.com";
}
class ChildClass extends ParentClass { //automatically inherits url
public void run() {
//im guessing this class is where you want to use url?
System.out.println(url);
}
}
//A class to start to program
class Main {
public static void main(String[] args) {
ChildClass child = new ChildClass();
child.run();
}
}
ChildClass会自动从ParentClass继承String,允许您在ChildClass中使用url,无需额外工作。
关于java - 从父类获取字符串到子类的概念?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25102051/