Variables 如何将变量作为参数传递

Variables 如何将变量作为参数传递,variables,parameters,processing,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 { // membe

我有两位代码

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

}
类树{
//成员变量
int m_lineLength;//乌龟线长度
int m_x;//初始x位置
int m_y;//初始y位置
浮动m_branchAngle;//分支处的海龟旋转
float m_initOrientation;//初始方向
字符串m_state;//初始状态
浮点m_scaleFactor;//分支比例因子
字符串m_F_rule;//F-规则替换
字符串m_H_规则;//H规则替换
字符串m_f_rule;//f-规则替换
int m_numIterations;//要替换的次数
//建造师
//(d=线条长度,x&y=图纸的起始位置)
树(整数d,整数x,整数y){
m_线宽=d;
m_x=x;
m_y=y;
m_branchAngle=(25.7/180.0)*π;
m_initOrientation=-HALF_PI;
m_scaleFactor=1;
m_state=“F”;
m_F_rule=“F[+F]F[-F]F”;
m_H_rule=“”;
m_f__规则=”;
m_数量=5;
//在初始状态下执行L轮替换
对于(int k=0;k
这不是经过评估的家庭作业,这是一个章节结束的练习,但我被卡住了

我想“扩展树构造函数,以便所有树成员变量的值都可以作为参数传入。”

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

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


任何指向正确方向的指针都将不胜感激。

您已经拥有了向
树添加更多内容的一切

您可以看到,在
设置()中调用:

tree = new Tree(d, x, y); 
现在,该行实际上正在调用此处实现的构造函数:

Tree(int d, int x, int y) {
    m_lineLength = d;
    m_x = x;
    etc....
因此,如果需要,可以更改该构造函数以接受要从
setup()

例如,
Tree(intd、intx、inty、字符串字、浮点数、双bigNumber)

试着用它来做实验。如果你有任何问题,下午我

编辑

让我再加一点味道:

您可以看到构造函数是初始化类的方法。它与访问级别(
受保护、公共、私有)或构造函数的数量无关

例如,假设这个类有两个公共字段:

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米/秒^2

对吧??这是一个常数。但是,你将在你的问题中使用一个变量

同样的道理也适用于编程

你加了一行。这意味着现在,你可以用它来解决你的问题

现在,转到这一行

tree=新树(d,x,y)

并将其更改为:

Tree(int d, int x, int y, int iterations) {
    m_lineLength = d;
    m_x = x;
    ....
tree=新树(d,x,y,m_数量)

如您所见,现在您已经准备好“使用”树中的变量。然而!你还没做完。您还必须更新构造函数,因为如果不更新,编译器会抱怨

现在转到这一行

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;
    ....
你看,现在,你告诉你的树接受一个新的变量调用迭代,你是从其他地方设置的

然而!小心这有一个小问题:(

您没有任何关于该变量使用的代码