java - 从父类获取字符串到子类的概念?

标签 java inheritance

如何从父类获取字符串到子类?请检查我的代码并告诉我该怎么做?我想获取从父类到子类的字符串。

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/

相关文章:

java - 如何访问谷歌云数据流中压缩源的每个条目?并获取每个子文件的Byte[]

java - 按列分组并对 csv 文件进行排序 Spark

java - 在java中的String数组位置分配一个新值

java - 在 Eclipse 新 Java 类向导中看不到父类(super class)

c++ - 子类无法访问父类的变量

c# - 在 C# 中对抽象类使用接口(interface)

java - 创建一个输出目录的 Java 程序

java - 继承静态保护内部类,javac报错

c# - 在 OOP 的传统意义上不相关的类的继承

Perl:动态模块加载、对象继承和 "common helper files"