从 class in java 中的另一个方法调用方法时的循环行为

For loop behaviour when calling a method from another method in same class in java

请不要被我复杂的代码搞糊涂了。让我向您介绍我的代码和问题。

我构建了一个 class (CharDrawing),其中我在另一个方法 (stringDrawing) 中调用了一个方法 (charDrawing),比方说两个次,在下面的完整代码中。它正在返回我想要的对象。

现在,我正在尝试使用 for 循环替换 charDrawing() 方法的这 2 次调用。请参阅下面的评论部分。

sr=charDrawing(sr,x,y,w,h,pw,"G");
sr=charDrawing(sr,x+w+5,y,w,h,pw,"B");

    /*for (int i=0;i<l;i++)
    {                       
      sr=charDrawing(sr,x+i*(w+gap),y,w,h,pw,Character.toString(s.charAt(i)));
    }*/

但是我的 for 循环与上面两行的行为不一样?有什么区别?

应用 for 循环后我没有得到相同的结果。

调试中: 这些是我在这两种情况下对对象的调试结果。请参阅下面的完整代码。

代码 运行 没有 for 循环:

simple object line 1com.badlogic.gdx.graphics.glutils.ShapeRenderer@1ef3e2e
simple object line 2com.badlogic.gdx.graphics.glutils.ShapeRenderer@1ef3e2e

代码 运行 使用 for 循环:

for loop objectcom.badlogic.gdx.graphics.glutils.ShapeRenderer@13841b8
for loop objectcom.badlogic.gdx.graphics.glutils.ShapeRenderer@13841b8

我的分析: 如您所见,在这两种情况下,每一行都更新了相同的对象。那为什么我没有得到相同的结果?

完整代码:

public class CharDrawing {

    public ShapeRenderer stringDrawing(ShapeRenderer sr,float x, float y, float w, float h,float pw,float gap,String s) {

        int l=s.length();
        sr.begin(ShapeType.Filled);
        /*for (int i=0;i<l;i++)
        {                       
          sr=charDrawing(sr,x+i*(w+gap),y,w,h,pw,Character.toString(s.charAt(i)));
          System.out.println("for loop object"+sr);
        }*/
        sr=charDrawing(sr,x,y,w,h,pw,"G");
        System.out.println("simple object line 1"+sr);
        sr=charDrawing(sr,x+w+5,y,w,h,pw,"B");
        System.out.println("simple object line 2"+sr);
        sr.end();
        return sr;
    }
    public ShapeRenderer charDrawing(ShapeRenderer sr,float x, float y, float w, float h,float pw,String c)
    {

        if(c=="G")
        {
            //sr.begin(ShapeType.Filled);
            sr.setColor(200/255f, 245/255f,112/255f, 1);  //color1
            sr.rect(x,y,w,h);
            sr.setColor(107/255f, 107/255f,107/255f, 1);   //color2
            sr.rect(x+pw,y+pw,w*0.35f,h-2*pw);
            //sr.setColor(407/255f, 107/255f,107/255f, 1); //for debugging
            sr.rect(x+pw+w*0.35f,y+pw+h*0.5f-pw,0.65f*w-pw,h*0.5f-pw);
            sr.rect(x+2*pw+w*0.35f,y,0.65f*w-3*pw,h*0.5f-pw);       
            //sr.end();
        }

        if(c=="B")
        {
            //sr.begin(ShapeType.Filled);
            sr.setColor(200/255f, 245/255f,112/255f, 1);  //color1
            sr.rect(x,y,w,h);
            sr.setColor(107/255f, 107/255f,107/255f, 1);   //color2
            //sr.setColor(407/255f, 107/255f,107/255f, 1); //for debugging
            sr.rect(x+pw,y+pw,w-2*pw,0.5f*h-1.5f*pw);
            sr.rect(x+pw,y+0.5f*h+0.5f*pw,w-2*pw,0.5f*h-1.5f*pw);
            //sr.end();
        }

        return sr;
    }
}

仅供参考..我正在使用这个 libGDX ShapeRenderer api 来绘制一些形状。 http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/glutils/ShapeRenderer.html

抱歉,这是非常基本的概念,我无法理解

谢谢。

使用equals() 方法比较字符串。 “==”只检查引用而不检查字符串的内容。