Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使一个类在另一个类中可见?_Java_File_Class - Fatal编程技术网

Java 如何使一个类在另一个类中可见?

Java 如何使一个类在另一个类中可见?,java,file,class,Java,File,Class,我的问题是: 在ReadFile类中,我有一个 公共模块ReadTheFileGetTheModuleName(String fichier)方法,该方法必须从模块类返回对象 在该方法中,我从module类创建了一个模块,但是当我想要返回它时,我有一个错误:模块无法解析为变量 这是我的密码: public Module ReadTheFileGetTheModuleName(String fichier){ try{ InputStrea

我的问题是: 在ReadFile类中,我有一个
公共模块ReadTheFileGetTheModuleName(String fichier)
方法,该方法必须从模块类返回对象

在该方法中,我从module类创建了一个模块,但是当我想要返回它时,我有一个错误:模块无法解析为变量

这是我的密码:

public  Module ReadTheFileGetTheModuleName(String fichier){
            try{
                InputStream ips=new FileInputStream(fichier); 
                InputStreamReader ipsr=new InputStreamReader(ips);
                BufferedReader br=new BufferedReader(ipsr);
                String ligne;

                while ((ligne=br.readLine())!=null){
                    if (ligne.contains("module"))
                    {
                        /*String[] st = ligne.split(ligne, ' ');
                        System.out.println("Nom = "+st[1]);*/
                        int longueur = ligne.length();
                        ligne = ligne.substring(ligne.indexOf(" "));
                        ligne = ligne.trim();
                        System.out.println(ligne);

                        //putting the name in a Module Class
                        Module module = new Module();
                        module.setName(ligne);  
                        System.out.println("nom du module : "+module.getName());
                        module.setFichier(fichier);
                        System.out.println("nom du fichier lié au module : "+module.getFichier());

                    }

                    chaine+=ligne+"\n";

                }
                return module; //Here is the line were I have an error
                br.close();

            }       
            catch (Exception e){
                System.out.println(e.toString());
            }
**

有人知道我可能犯的错误吗?

问题是您在
if
循环的作用域内,在
while
循环内声明
模块
变量。这意味着位于外部范围内的
return
语句不知道该声明

例如,这段代码将失败:

int outerVariable = 5;
if (outerVariable == 5) {
    // Ok: the inner scope can access all variables declared in the outer scopes
    int innerVariable = 1 + outerVariable;
}
// Ok: the println call and outerVariable declaration are in the same scope
System.out.println(outerVariable);

// Error: The outer scope can't access a variable declared in a inner scope
System.out.println(innerVariable);
为了修复代码,您需要将
模块
的声明移动到外部范围,或将
返回
移动到内部范围。移动到外部范围将如下所示:

public Module ReadTheFileGetTheModuleName(String fichier) {
    try {
        Module module = new Module();
        while (...) {
            if (...) {
                // Set the appropriate module properties
            }
        }
        // Ok: the return statement can now see the module declaration
        return module;
    } catch (...) {
        ...
    }
    // Error: no return statement   
}
public Module ReadTheFileGetTheModuleName(String fichier) {
    Module module = new Module();
    try {
        while (...) {
            if (...) {
                // Set the appropriate module properties
            }
        }
    } catch (...) {
        ...
    }    
    return module;    
}
请注意,仍然会出现错误,因为在try-catch块之外没有return语句。您需要决定在这种情况下要做什么,您可以返回
null
,也可以将
模块
声明移出try catch并返回,如下所示:

public Module ReadTheFileGetTheModuleName(String fichier) {
    try {
        Module module = new Module();
        while (...) {
            if (...) {
                // Set the appropriate module properties
            }
        }
        // Ok: the return statement can now see the module declaration
        return module;
    } catch (...) {
        ...
    }
    // Error: no return statement   
}
public Module ReadTheFileGetTheModuleName(String fichier) {
    Module module = new Module();
    try {
        while (...) {
            if (...) {
                // Set the appropriate module properties
            }
        }
    } catch (...) {
        ...
    }    
    return module;    
}

问题是您在嵌套范围内声明
模块
变量,但试图在该范围外使用它:

while (...) {
    if (...) {
        Module module = ...;
    }
}
return module;
如果
while
if
条件从未计算为
true
,您预计会发生什么情况?如果您想在这种情况下返回
null
(或其他一些默认值),这很简单-只需在循环之前声明它:

Module module = ...;
while (...) {
    if (...) {
        module = ...;
    }
}
return module;
或者您真的应该将
return
语句移动到
if
语句中:

while (...) {
    if (...) {
        Module module = ...;
        ...
        return module;
    }
}
// Work out what you want to do here

这与涉及的不同类无关,只是范围问题。

另一个选项是在创建对象时在循环中返回


然后在循环之外,您必须返回
null

您正在while循环内创建
模块
。在圈外创建它谢谢你这是解决方案!