【问题标题】:Using One Variable Parameter Inside Another Method在另一种方法中使用一个变量参数
【发布时间】:2018-02-04 11:02:07
【问题描述】:

所以我试图为我的类创建一些名为 test 的方法,这些方法从驱动程序中获取一个变量。有一些方法应该从 getter 方法中获取返回值。

主类:

public class test {
    public int setSpeed(int speed) {
        return speed;
    }
    public void getSpeed() {
        //I don't know what to put here
    }
}

司机:

public class testDriver {
    test b = new test();
    b.setSpeed(25);
    System.out.println("The Speed is: ", b.getSpeed());
}

我必须在驱动程序中使用常规 int;我无法将其值分配给变量。

【问题讨论】:

    标签: java methods parameters return


    【解决方案1】:

    您的 test 类应该如下所示:

    public class test {
        private int speed;
        public void setSpeed(int speed) {
            this.speed = speed;
        }
        public int getSpeed() {
            return this.speed;
        }
    }
    

    基本上,您在设置值时不应该返回某些内容。你假设当你得到一些东西时返回一个值

    【讨论】:

    • 是的,非常感谢,这更有意义!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-18
    • 1970-01-01
    • 2017-02-03
    相关资源
    最近更新 更多