简体   繁体   English

如何在Java中的其他方法中使用此构造函数中的变量?

[英]How do I use variables from this constructor in other methods in Java?

So I have a constructor that looks like this 所以我有一个看起来像这样的构造函数

public Maze(String[] textmaze, int startRow, int startCol, int finishRow, int finishCol){

I want to access the variables startRow, startCol, finishRow, and finishCol however those variables can only be passed in as parameters from another class(which I can't modify). 我想访问变量startRow,startCol,finishRow和finishCol,但是这些变量只能作为参数从另一个类传递(我不能修改)。 So how would I use those variables from the constructor in another method? 那么,如何在另一个方法中使用构造函数中的那些变量呢?

You could use something like this: 您可以使用如下形式:

class Maze{
    private int startRow, startCol, finishRow, finishCol;

    public Maze(String[] textmaze, int startRow, int startCol, int finishRow, int finishCol){
        this.startRow = startRow;
        this.startCol = startCol;
        this.finishRow = finishRow;
        this.finishCol = finishCol;
    }

    private void someMethod(){
        System.out.println(startRow);
    }

}

You can save them to properties or fields. 您可以将它们保存到属性或字段。 Here's the documentation about properties . 这是有关properties的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM