Hi,
I’m learning to program with Processing and a question came to me !
I created a class called Estrela (Star) and I created some private attributes in it, but I’m noticing that I can access these private variables from my main file … Is this normal?
//The Estrela class
class Estrela
{
private float x,y;
private float speed;
private color cor;
Estrela(float x, float y, float speed, color cor)
{
this.x = x;
this.y = y;
this.speed = speed;
this.cor = cor;
}
void Draw()
{
strokeWeight(3);
stroke(cor);
point(x, y);
x = x - speed;
if(x < 0)
{
x = width;
}
}
}
//the main file
import processing.sound.*;
color[] corEstrelas = {#626262, #898787, #CBC9C9, #ffffff };
Estrela[] estrelas;
void setup()
{
size(800,600);
inicializaEstrelas();
}
void draw()
{
clear();
background(0);
for(Estrela estrela: estrelas)
{
//a test -> Why can I acess this private variable of Estrela instance
estrela.x = 10;// <-- x is a private variable but this give no error ??!!
//---------------------
estrela.Draw();
}
}
void inicializaEstrelas()
{
estrelas= new Estrela[600];
for(int i=0;i<estrelas.length;i++)
{
int x = int(random(0,width));
int y = int(random(0,height));
float speed = random(0.5f,5);
color cor = corEstrelas[int(random(0,corEstrelas.length))];
//int x, int y, float speed, color cor
estrelas[i] = new Estrela(x,y,speed,cor);
}
}