如何在Java中存储变量?

时间:2016-12-22 17:04:11

标签: java

我正在学习,我正在尝试创建一个简单的功能。变量another_unsafe_operation(UpdatingSystem const & lock, int data) { // Do stuff, you don't even need to use the lock for anything. } 应该生成随机数,同时应该每次都将数字添加到变量p1

s1

我通过

调用了方法
class snake{
    double p1,position1;
    void testing(){
        p1=Math.random();
        p1= p1*100;
        System.out.println(p1);
    }
}
class helper extends snake{
    double s1;
    helper(){
        s1=0;
    }
    void make_changes(){
        s1=s1+p1;
        System.out.println(s1);
    }
}

但是当我运行代码时,值不会添加helper h1 = new helper(); h1.testing(); h1.make_changes(); s1具有相同的值。

2 个答案:

答案 0 :(得分:1)

代码工作得很好。初始化helper时,s10。每次拨打make_changes都会向其添加p1。您在此处的代码段中分享的第一个调用将使s1等于0+p1,这只是p1

答案 1 :(得分:0)

您将s1初始化为零:

helper(){
    s1=0;
    }

所以s1将是p1加零,相当于p1。