Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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_Variables - Fatal编程技术网

在Java层次结构中的任意位置访问变量

在Java层次结构中的任意位置访问变量,java,variables,Java,Variables,我想从另一个方法访问方法内部的变量集。我正在尝试访问方法入口中的扫描仪sc,但无法解析变量 public class LineReader extends MaxObject { public LineReader() { declareInlets(new int[]{DataTypes.INT,DataTypes.ALL}); declareOutlets(new int[]{DataTypes.INT,DataTypes.ALL}); }

我想从另一个方法访问方法内部的变量集。我正在尝试访问方法
入口
中的
扫描仪
sc
,但无法解析变量

public class LineReader extends MaxObject {

    public LineReader() {
        declareInlets(new int[]{DataTypes.INT,DataTypes.ALL});
        declareOutlets(new int[]{DataTypes.INT,DataTypes.ALL});
    }

    public void input(String Fold.sc, Atom[] args) {
        Scanner sc = new Scanner(new File());
        if (getInlet() == 1) {
            post("hello anything " + Fold.sc + " " + Atom.toOneString(args) + "!");
            outlet(1, s, args);
        } else {
            post("uh"); 
        }
    }

    public void inlet(int a) {
        for (int i = 0; i < startLine; i++) { 
            info = sc.readLine(); 
        }
        for (int i = startLine; i < endLine + 1; i++) {
            info = sc.readLine();
            System.out.println(info);
            post("hello integer " + a + "!");
            outlet(0, info);
        }       
    } 
}
公共类LineReader扩展了MaxObject{
公共线路阅读器(){
declareInlet(新的int[]{DataTypes.int,DataTypes.ALL});
declareOutlets(newint[]{DataTypes.int,DataTypes.ALL});
}
公共无效输入(字符串Fold.sc,Atom[]args){
Scanner sc=新扫描仪(新文件());
如果(getInlet()==1){
post(“hello anything”+Fold.sc+“”+Atom.toOneString(args)+”!”;
出口(1,s,args);
}否则{
邮政("嗯");;
}
}
公共排水口(内部a){
对于(int i=0;i
将扫描仪SC声明为输入功能的外侧

public class LineReader extends MaxObject {
Scanner sc;
public LineReader() {
declareInlets(new int[]{DataTypes.INT,DataTypes.ALL});
declareOutlets(new int[]{DataTypes.INT,DataTypes.ALL});
}
public void input(String Fold.sc, Atom[] args) {
    sc = new Scanner(new File());
        if (getInlet() == 1){
        post("hello anything " + Fold.sc + " " + Atom.toOneString(args) + "!");
        outlet(1, s, args);
    } else
        post("uh"); 
}
public void inlet(int a) {

for (int i = 0; i < startLine; i++) { info = sc.readLine(); }
for (int i = startLine; i < endLine + 1; i++) {
info = sc.readLine();
System.out.println(info);
post("hello integer " + a + "!");
outlet(0, info);
        }       
    }
}
公共类LineReader扩展了MaxObject{
扫描仪sc;
公共线路阅读器(){
declareInlet(新的int[]{DataTypes.int,DataTypes.ALL});
declareOutlets(newint[]{DataTypes.int,DataTypes.ALL});
}
公共无效输入(字符串Fold.sc,Atom[]args){
sc=新扫描仪(新文件());
如果(getInlet()==1){
post(“hello anything”+Fold.sc+“”+Atom.toOneString(args)+”!”;
出口(1,s,args);
}否则
邮政("嗯");;
}
公共排水口(内部a){
对于(inti=0;i
将扫描仪SC声明为输入功能的外侧

public class LineReader extends MaxObject {
Scanner sc;
public LineReader() {
declareInlets(new int[]{DataTypes.INT,DataTypes.ALL});
declareOutlets(new int[]{DataTypes.INT,DataTypes.ALL});
}
public void input(String Fold.sc, Atom[] args) {
    sc = new Scanner(new File());
        if (getInlet() == 1){
        post("hello anything " + Fold.sc + " " + Atom.toOneString(args) + "!");
        outlet(1, s, args);
    } else
        post("uh"); 
}
public void inlet(int a) {

for (int i = 0; i < startLine; i++) { info = sc.readLine(); }
for (int i = startLine; i < endLine + 1; i++) {
info = sc.readLine();
System.out.println(info);
post("hello integer " + a + "!");
outlet(0, info);
        }       
    }
}
公共类LineReader扩展了MaxObject{
扫描仪sc;
公共线路阅读器(){
declareInlet(新的int[]{DataTypes.int,DataTypes.ALL});
declareOutlets(newint[]{DataTypes.int,DataTypes.ALL});
}
公共无效输入(字符串Fold.sc,Atom[]args){
sc=新扫描仪(新文件());
如果(getInlet()==1){
post(“hello anything”+Fold.sc+“”+Atom.toOneString(args)+”!”;
出口(1,s,args);
}否则
邮政("嗯");;
}
公共排水口(内部a){
对于(inti=0;i
您的扫描仪实例是输入法的本地实例。您需要将实例传递给入口方法才能在那里使用它

public void input(String Fold.sc, Atom[] args) {
 ...
    Scanner sc = new Scanner(new File());
 ... 
}
public void inlet(int a, Scanner sc) {
...
    for (int i = 0; i < startLine; i++) { info = sc.readLine(); }
...
}
public void输入(String Fold.sc,Atom[]args){
...
Scanner sc=新扫描仪(新文件());
... 
}
公共空隙入口(内部a、扫描仪sc){
...
对于(inti=0;i
您的扫描仪实例是输入法的本地实例。您需要将实例传递给入口方法才能在那里使用它

public void input(String Fold.sc, Atom[] args) {
 ...
    Scanner sc = new Scanner(new File());
 ... 
}
public void inlet(int a, Scanner sc) {
...
    for (int i = 0; i < startLine; i++) { info = sc.readLine(); }
...
}
public void输入(String Fold.sc,Atom[]args){
...
Scanner sc=新扫描仪(新文件());
... 
}
公共空隙入口(内部a、扫描仪sc){
...
对于(inti=0;i
您似乎在理解上有困难。花点时间了解一下可能会对你有好处

由于变量
sc
是在方法
input
中声明的,因此只能在那里访问。如果希望它在整个类中都可以访问,那么应该使它成为类的成员

下面是一个如何工作的示例:

public class MyClass {
    int x = 3;

    public void method1() {
        int a = 1;
    }

    public void method2() {
        System.out.println(a); //will not work - a is not in scope
        System.out.println(x); //will work - x is accessible from all methods
    }
}

看来你在理解上有困难。花点时间了解一下可能会对你有好处

由于变量
sc
是在方法
input
中声明的,因此只能在那里访问。如果希望它在整个类中都可以访问,那么应该使它成为类的成员

下面是一个如何工作的示例:

public class MyClass {
    int x = 3;

    public void method1() {
        int a = 1;
    }

    public void method2() {
        System.out.println(a); //will not work - a is not in scope
        System.out.println(x); //will work - x is accessible from all methods
    }
}

请重新表述这个问题,并指出您在代码中尝试执行任何操作的位置。这样很难理解这个问题。另外,下次请缩进代码,这样就不会太混乱了。这段代码太长,太混乱了。你能把它精简到基本部分吗?我正在尝试访问公共void入口中的scanner sc,但变量无法解决。请重新表述这个问题,并在代码中指出你正在尝试执行任何操作的位置。这样很难理解这个问题。另外,下次请缩进代码,这样就不会太混乱了。这段代码太长,太混乱了。你能把它缩小到基本部分吗?我正试图访问公共空隙入口中的扫描仪sc,但变量无法解析。