小编mog*_*gli的帖子

子shell进程无法访问父shell中定义的变量和函数

我在同一个目录中有 3 个脚本,请在下面找到 x.sh、y.sh 和 z.sh 的内容:-

x.sh :-

xData="DataOfX"

function xInit(){
    echo "xInit : data of a >$xData<"
}
Run Code Online (Sandbox Code Playgroud)

y.sh :-

. x.sh

xInit

sh z.sh zInit
Run Code Online (Sandbox Code Playgroud)

z.sh :-

function zInit(){
    echo "zInit of z"
    xInit
}

$@
Run Code Online (Sandbox Code Playgroud)

执行

. y.sh

在同一目录中给出以下输出:-

xInit : data of a >DataOfX<
zInit of z
z.sh: line 3: xInit: command not found
Run Code Online (Sandbox Code Playgroud)

子 shell 进程如何访问在父 shell 中初始化的变量和函数?

variables bash shell function subshell

2
推荐指数
1
解决办法
1570
查看次数

为什么界面中有Object类方法?

以下接口和类已成功编译.问题在下面的输出中提到:

interface MyInterface{}

class MyClass implements MyInterface{}

class InterDoubt{

    static MyInterface mi ;//= new MyClass() ;

    public static void main(String[] args){
        System.out.println("X") ;

        try{
            synchronized(mi){
                try{
                    mi.wait(4000) ;
                }
                catch(InterruptedException ie){
                    System.out.println("Exception occured at main.") ;
                }
            }
        }
        catch(Exception e){
            System.out.println("voilla, MyInterface is an interface,\n" + 
                       "then why compiler allows compilation of\n" +
                       "mi.getClass(), mi.wait().\n" +
                       "Or how the methods of Object class are available in an interface."
            );
        }

        System.out.println("Y") ;
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

X

voilla,MyInterface是一个界面,

那么为什么编译器允许编译

mi.getClass(),mi.wait(). …

java

1
推荐指数
1
解决办法
1110
查看次数

什么是MyClass.class?

以下是MainClass.java中列出的代码.

public class MainClass {

    public static void main(String[] args) {

        System.out.println("main started...");

        Class c = MyClass.class ;
                        //this class variable seems to be public static.
                        //But, as it is clearly visible in the MyClass,
                        //no reference variable is declared.
                        //My problem is that from where this class variable
                        //came from.
                        //i also check out the Object.java file, but it also don't
                        //have any public static class variable of Class class
                        //like there is
                        //out (instance of PrintStream class) in …
Run Code Online (Sandbox Code Playgroud)

java

0
推荐指数
1
解决办法
6978
查看次数

为什么文件中没有所有条目?

在下面的程序中,循环迭代1000次,我使用FileWriter在文件中写入所有条目,但不幸的是程序最终只在文件中写入510(有时是415,有时是692,总是少于1000)条目,但循环迭代1000次.

import java.io.* ;
import java.util.*;

public class DemoWriter {

    public static void main(String[] args) throws Exception {

        List<String> receiverList = new ArrayList<String>() ;
        receiverList.add("abc@gmail.com") ;
        receiverList.add("pqr@ibibo.com") ;
        receiverList.add("xyz@gmail.com") ;

        FileWriter fw = new FileWriter("a.txt") ;
        BufferedWriter bw = new BufferedWriter(fw) ;

        int size = receiverList.size() ;

        String str ;
        int count = 0 ;
        for(int i = 1 ; i <= 1000 ; ++i){
            str = receiverList.get( (int) (Math.random() * size) ) + "\n" ;
            bw.write(++count + ".> " …
Run Code Online (Sandbox Code Playgroud)

java file-io

0
推荐指数
2
解决办法
497
查看次数

标签 统计

java ×3

bash ×1

file-io ×1

function ×1

shell ×1

subshell ×1

variables ×1