【问题标题】:How do I use variables from this constructor in other methods in Java?如何在 Java 的其他方法中使用此构造函数中的变量?
【发布时间】:2015-05-06 01:42:15
【问题描述】:

所以我有一个看起来像这样的构造函数

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

我想访问变量 startRow、startCol、finishRow 和 finishCol,但是这些变量只能作为另一个类的参数传入(我无法修改)。那么我将如何在另一种方法中使用构造函数中的这些变量呢?

【问题讨论】:

    标签: java class variables methods constructor


    【解决方案1】:

    你可以这样使用:

    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);
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      您可以将它们保存到属性或字段中。这是关于properties 的文档。

      【讨论】:

        猜你喜欢
        • 2016-04-08
        • 2012-11-10
        • 2016-05-09
        • 2016-01-06
        • 2018-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多