2015-03-06 2 views
0

У меня есть класс и основной код, перечисленные ниже.Конструктор не передает значения (processing.org)

Я могу изменить значения вручную в конструкторе, но как я могу расширить конструктор так, чтобы значения для всех переменных дерева передавались как параметры?

Я сделал все чтение, которое я могу найти повторно. конструкторы, но я все еще очень застрял. Я использую processing.org (Java-эск) и являюсь n00b - ответьте, пожалуйста, используя маленькие слова :)

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

} 

и

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

Большое спасибо,

@LuisLavieri - I изучает ваш ответ на мой предыдущий вопрос, но все еще застрял! Я попытался начать с вами чат (после вашего любезного предложения), но не мог понять, как это сделать.

ответ

1

Вы просто добавили бы их в качестве параметров в конструкторе. Вот пример:

Tree(int d, int x, int y, int one, String two, boolean three) { 

И тогда, когда вы называете этот конструктор, вы просто должны предоставить значения для этих параметров:

tree = new Tree(d, x, y, 1, "two", false); 

Рекомендуемая литература: http://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html