gpt4 book ai didi

Java:对象声明

转载 作者:行者123 更新时间:2023-12-02 04:17:49 26 4
gpt4 key购买 nike

在Java中声明对象时:

Runtime rt = Runtime.getRuntime();
Process lsProc = rt.exec("who -q");
InputStream in = lsProc.getInputStream();

为什么 lsproc 没有这样声明 -> Process lsproc = new Process?对象 lsproc 如何保存另一个函数的值?

最佳答案

 Process lsProc = rt.exec("who -q");

这行本身就意味着,

这意味着 Runtime 类的 exec 方法返回 Process 类型的实例 IsProc

看看source code of exec method

public Process exec(String[] cmdarray, String[] envp, File dir)
609 throws IOException {
610 return new ProcessBuilder(cmdarray)
611 .environment(envp)
612 .directory(dir)
613 .start();
614 }

它返回 ProcessBuilder 的实例,其类型为 Process,这意味着 Process 是一个抽象类,而 ProcessBuilder 是它的具体类。

现在你可能会遇到一个问题:如何

Process pro = new ProcessBuilder(..); 

有效,它会引导您阅读有关多态性的内容。

关于Java:对象声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33119778/

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