如何将变量作为参数传递
How to pass variables as parameters
我有两位代码
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;
}
}
这不是评估的作业,它是章节练习的结尾,但我很困惑。
我要"extend the Tree constructor so that values for all of the Tree member variables can be passed in as parameters."
虽然我了解什么是变量和参数,但我对从什么开始阅读/从哪里开始编辑代码感到很困惑。
让我困惑并质疑我的理解的一件事是,如果我更改构造函数值(例如 m_numiterations = 10;),代码为 [=28= 时的输出]也是一样。
任何正确方向的指示将不胜感激。
您已经拥有了向 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)
试试看。如果有任何问题,PM我
编辑
让我给它添加一点风味:
您会看到构造函数是初始化 class 的方式。访问级别 (protected, public, private
) 或构造函数的数量无关紧要。
因此,例如,假设您有这个 class 和两个 public 字段:
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
}
}
在这里,您可以创建带有作者和书名或只有书名的书籍!那不是很好吗?您可以创建不包含附加到其他事物的事物!
希望您能理解这一点。但是,基本上这个想法是将与某个主题相关的所有内容封装到它自己的 class.
新编辑
迈克,你看,根据你的评论,你添加了这一行:
int m_numIterations = 25;
问题是您刚才所做的只是创建了一个变量。变量保存您最终要在程序中使用的信息。假设你正在学习高中物理,试图解决一个基本的自由落体问题。你必须陈述重力,不是吗?
所以,在你的笔记本中,你会去:
g = 9.8 m/s^2
对吧?它是一个常数。但是,您将在问题中使用的变量。
嗯,同样的道理也适用于编程。
您添加了该行。这意味着现在,您可以在您的问题中使用它了。
现在,转到这一行,
tree = new Tree(d, x, y);
并将其更改为:
tree = new Tree(d, x, y, m_numIterations);
如您所见,现在您已准备好 "use" 树中的变量。然而!你还没有完成。您还必须更新构造函数,否则编译器会报错!
现在转到这一行,
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;
....
你看,现在你告诉你的树接受你从其他地方设置的新变量调用迭代。
不过!警告! 这个有点问题:(
您没有任何关于使用该变量的代码。所以,如果您期望在 Tree 中真正看到不同的东西,那是不会发生的!您需要在树的范围内找到变量的用途(我称之为 iterations
的那个)。所以,首先,找到它的用途!或 post 您需要的任何更多代码来帮助您解决问题。如果您正在调用变量迭代,那是因为您打算在某处使用循环,amirite?保重男人。小步骤。要有耐心。我在 Books 示例中添加了一些内容。昨天忘记解释了:p
我有两位代码
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;
}
}
这不是评估的作业,它是章节练习的结尾,但我很困惑。
我要"extend the Tree constructor so that values for all of the Tree member variables can be passed in as parameters."
虽然我了解什么是变量和参数,但我对从什么开始阅读/从哪里开始编辑代码感到很困惑。
让我困惑并质疑我的理解的一件事是,如果我更改构造函数值(例如 m_numiterations = 10;),代码为 [=28= 时的输出]也是一样。
任何正确方向的指示将不胜感激。
您已经拥有了向 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)
试试看。如果有任何问题,PM我
编辑
让我给它添加一点风味:
您会看到构造函数是初始化 class 的方式。访问级别 (protected, public, private
) 或构造函数的数量无关紧要。
因此,例如,假设您有这个 class 和两个 public 字段:
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
}
}
在这里,您可以创建带有作者和书名或只有书名的书籍!那不是很好吗?您可以创建不包含附加到其他事物的事物!
希望您能理解这一点。但是,基本上这个想法是将与某个主题相关的所有内容封装到它自己的 class.
新编辑
迈克,你看,根据你的评论,你添加了这一行:
int m_numIterations = 25;
问题是您刚才所做的只是创建了一个变量。变量保存您最终要在程序中使用的信息。假设你正在学习高中物理,试图解决一个基本的自由落体问题。你必须陈述重力,不是吗?
所以,在你的笔记本中,你会去:
g = 9.8 m/s^2
对吧?它是一个常数。但是,您将在问题中使用的变量。
嗯,同样的道理也适用于编程。
您添加了该行。这意味着现在,您可以在您的问题中使用它了。
现在,转到这一行,
tree = new Tree(d, x, y);
并将其更改为:
tree = new Tree(d, x, y, m_numIterations);
如您所见,现在您已准备好 "use" 树中的变量。然而!你还没有完成。您还必须更新构造函数,否则编译器会报错!
现在转到这一行,
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;
....
你看,现在你告诉你的树接受你从其他地方设置的新变量调用迭代。
不过!警告! 这个有点问题:(
您没有任何关于使用该变量的代码。所以,如果您期望在 Tree 中真正看到不同的东西,那是不会发生的!您需要在树的范围内找到变量的用途(我称之为 iterations
的那个)。所以,首先,找到它的用途!或 post 您需要的任何更多代码来帮助您解决问题。如果您正在调用变量迭代,那是因为您打算在某处使用循环,amirite?保重男人。小步骤。要有耐心。我在 Books 示例中添加了一些内容。昨天忘记解释了:p