我解釋我的問題:我試圖解決一個數組有問題,我有一個程序在OOP中完成,其中有一個類叫做lights。我想要的是做一個數組與內部的PIN碼,並將其設置爲輸出。我做了一針一針,這很容易做到,但現在我想用一個for循環和一個數組來做到這一點 我不知道如何構建一個構造函數和處理這個數組與白色針數字在其中。數組,構造函數和實例
//----------------------------------------------------------------------------------
class lights{
int pins[5]; //array for 5 elements
int i;
public:
lights(int ledPins[]){
for(i=0; i<5; i++){ //pins are set up as output
pinMode(pins[i],OUTPUT);
}
}//end constructor
void attempt(){ //metodo para pobrar si se enciende los leds
for(i=0; i<5; i++){
digitalWrite(pins[i],HIGH);
Serial.println(pins[i]);
}
}//end attempt
};//end class
//---------------------------------------------------------------------------------------------------------------------
int MyPins[] = {5,6,7,8,9}; //I declare what it is inside
lights lit(MyPins); //I create an objet with my array as parameters
void setup(){
Serial.begin(9600);
}
void loop(){
lit.attempt();
}
Java和C++是兩種截然不同的語言。選一個。 –
@OusmaneMahyDiaw OP已經標記了java和C++的問題,這是山姆所指的 – Ishnark