その中のプロパティ禁止の練習をしています
クラスは5つです(Component Velocity Location X Y)
下記がソースになります
class X{
private:
int value;
public:
void incrementValue( X x ){
//value += x.getValue();
}
};
class Y{
private:
int value;
public:
void incrementValue( Y y ){
//value += y.getValue();
}
};
class Velocity{
private:
X x;
Y y;
public:
};
class Location{
private:
X x;
Y y;
public:
void incrementX( Velocity velocity ){
this->x.incrementValue( /*velocity.x*/ );
}
void incrementY( Velocity velocity ){
this->y.incrementValue( /*velocity.y*/ );
}
};
class Component{
private:
Location location;
Velocity velocity;
public:
void Move(){
this->location.incrementX( this->velocity );
this->location.incrementY( this->velocity );
}
};
int main(){
Component *component = new Component();
component->Move();
return 0;
}
ゲッターありで書いた場合には下記の通りになると予想しています
class X{
private:
int value;
public:
void incrementValue( X x ){
value += x.getValue();
}
int getValue(){
return this->value;
}
};
class Y{
private:
int value;
public:
void incrementValue( Y y ){
value += y.getValue();
}
int getValue(){
return this->value;
}
};
class Velocity{
private:
X x;
Y y;
public:
X getX(){ return this->x; }
Y getY(){ return this->y; }
};
class Location{
private:
X x;
Y y;
public:
void incrementX( Velocity velocity ){
this->x.incrementValue( this->x );
}
void incrementY( Velocity velocity ){
this->y.incrementValue( this->y );
}
};
class Component{
private:
Location location;
Velocity velocity;
public:
void Move(){
this->location.incrementX( this->velocity );
this->location.incrementY( this->velocity );
}
};
int main(){
Component *component = new Component();
component->Move();
return 0;
}