如何将变量作为参数传递

时间:2015-03-05 03:58:00

标签: variables parameters processing

我有两位代码

Tree tree;

void setup() {
  int SZ = 512;  // screen size

  int d = 2;
  int x = SZ/2;
  int y = SZ;

  size(SZ,SZ);
  background(255);
  noLoop();

  tree = new Tree(d, x, y);  
}

void draw() {
  tree.draw();
}

以及

class Tree {

  // member variables
  int    m_lineLength;       // turtle line length
  int    m_x;                // initial x position
  int    m_y;                // initial y position
  float  m_branchAngle;      // turtle rotation at branch
  float  m_initOrientation;  // initial orientation
  String m_state;            // initial state
  float  m_scaleFactor;      // branch scale factor
  String m_F_rule;           // F-rule substitution
  String m_H_rule;           // H-rule substitution
  String m_f_rule;           // f-rule substitution
  int    m_numIterations;    // number of times to substitute

  // constructor
  // (d = line length, x & y = start position of drawing)
  Tree(int d, int x, int y) {
    m_lineLength = d;
    m_x = x;
    m_y = y; 
    m_branchAngle = (25.7/180.0)*PI;
    m_initOrientation = -HALF_PI;
    m_scaleFactor = 1;
    m_state = "F";
    m_F_rule = "F[+F]F[-F]F";
    m_H_rule = "";
    m_f_rule = "";
    m_numIterations = 5;

    // Perform L rounds of substitutions on the initial state
    for (int k=0; k < m_numIterations; k++) {
      m_state = substitute(m_state);
    }
  }

  void draw() {
    pushMatrix();
    pushStyle();

    stroke(0);
    translate(m_x, m_y);        // initial position
    rotate(m_initOrientation);  // initial rotation

    // now walk along the state string, executing the
    // corresponding turtle command for each character
    for (int i=0; i < m_state.length(); i++) {
      turtle(m_state.charAt(i));
    }

    popStyle();
    popMatrix();
  }

  // Turtle command definitions for each character in our alphabet
  void turtle(char c) {
    switch(c) {
    case 'F': // drop through to next case
    case 'H':
      line(0, 0, m_lineLength, 0);
      translate(m_lineLength, 0);
      break;
    case 'f':
      translate(m_lineLength, 0);
      break;
    case 's':
      scale(m_scaleFactor);
      break;
    case '-':
      rotate(m_branchAngle);
      break;
    case '+':
      rotate(-m_branchAngle);
      break;
    case '[':
      pushMatrix();
      break;
    case ']':
      popMatrix();
      break;
    default:
      println("Bad character: " + c);
      exit();
    }
  }

  // apply substitution rules to string s and return the resulting string
  String substitute(String s) {
    String newState = new String();
    for (int j=0; j < s.length(); j++) {
      switch (s.charAt(j)) {
      case 'F':
        newState += m_F_rule;
        break;
      case 'H':
        newState += m_F_rule;
        break;
      case 'f':
        newState += m_f_rule;
        break;
      default:
        newState += s.charAt(j);
      }
    }
    return newState;
  }

}

这不是评估作业,它是章节练习的结束,但我很困难。

我希望&#34;扩展Tree构造函数,以便可以将所有Tree成员变量的值作为参数传入。&#34;

虽然我了解变量和参数是什么,但我对于从什么开始阅读/从何处开始编辑代码感到困惑。

让我困惑的一件事让我质疑我的理解是,如果我更改构造函数值(例如m_numiterations = 10;),则运行代码时的输出是相同的。

任何指向正确方向的人都会非常感激。

1 个答案:

答案 0 :(得分:3)

您已经拥有了所有内容,可以为Tree添加更多内容。

您可以在setup()中致电:

tree = new Tree(d, x, y); 

现在,该行实际上是调用此处实现的构造函数:

Tree(int d, int x, int y) {
    m_lineLength = d;
    m_x = x;
    etc....

因此,如果您愿意,可以更改该构造函数以接受您想要从setup()传递的任何变量

例如,Tree(int d, int x, int y, String word, float number, double bigNumber)

尝试尝试。如果您有任何疑问,请联系我

修改

让我为它添加更多的味道:

您会看到构造函数是初始化类的方法。访问级别(protected, public, private)或构造函数的数量无关紧要。

因此,举例来说,让我们说这个课有两个公共领域:

public class Book
{
    public String Author;
    public String Title;
    public Book(String title, String author)
    {
        this.Title = title;
        this.Author = author;
    }

    public Book()
    {
        this("Any title");//default title
    }
}

在这里,您可以创建既包含作者又包含标题的书籍,或者只创建标题!那不是很好吗?你可以创造一些不包含在其他东西中的东西!

我希望你明白这一点。但是,基本上这个想法是将与某个主题无关的所有内容封装到自己的类中。

新编辑

迈克,你看,根据你的评论你添加了这一行:

int m_numIterations = 25;

问题是你刚刚做的只是创建一个变量。变量保存您最终要在程序中使用的信息。让我们说你在高中物理学中试图解决一个基本的自由落体问题。你必须陈述引力,不是吗?

所以,在你的笔记本中,你会去:

g = 9.8 m/s^2

正确?这是一个常数。但是,您将在问题中使用的变量。

嗯,同样的事情适用于编程。

您添加了该行。这意味着现在,您可以在问题中使用它。

现在,转到这一行,

tree = new Tree(d, x, y);

并将其更改为:

tree = new Tree(d, x, y, m_numIterations);

正如您所看到的,现在您已准备好&#34;使用&#34;树中的变量。然而!你还没有完成。你必须更新你的构造函数,因为如果没有,编译器会抱怨!

立即转到此行,

Tree(int d, int x, int y) {
    m_lineLength = d;
    m_x = x;
    ....

并将其更改为:

Tree(int d, int x, int y, int iterations) {
    m_lineLength = d;
    m_x = x;
    ....

您,现在,您正在告诉您的树接受您从其他地方设置的新变量调用迭代。

但是!请注意!这有一点问题:(

您没有关于该变量使用的任何代码。所以,如果你期望在树中看到不同的东西,它就不会发生!您需要在树的范围内找到对变量的使用(我称之为iterations)。所以,首先,找到它的用途!或者发布任何你需要的代码来帮助你解决它。如果你正在调用变量迭代,那是因为你打算在某处使用循环,amirite?保重人。小步骤。耐心点。我在Books示例中添加了一些内容。我昨天忘了解释一下:p