문제

나는 항상 아래에있는 값의 출력을 제어하려고합니다. (X) 매번 논쟁을 부르지 않고 ::

작은 예 ~

public int CurrentSpeed;
public int MaxSpeed;
private int caracceleration;
private int Blarg;

public int CarAcceleration{
    get{ 
        Blarg = CurrentSpeed + caracceleration;
        if(Blarg >= MaxSpeed){
            Blarg = MaxSpeed
        }

        return Blarg

    set;
    }

매번 인수를 부르지 않고이 작업을 수행하는 더 좋은 방법이 있습니까 ?? 슬프게도 숫자의 양과 복잡성이 증가함에 따라 (내 코드에서 3D 배열을 사용하고 있습니다) 이것은 가벼운 병목 현상이됩니다

도움이 되었습니까?

해결책 2

public int Speed
{
  get
  {
     return CurrentSpeed + CarAcceleration;
  {
}

public int CarAcceleration{
    get
    { 
        if(Speed >= MaxSpeed)
        {
            return MaxSpeed
        }

        return Speed;
    }
    set;
    }

여러 곳에서 합산을 반복하지 않기 위해 계산을 롤업 할 수 있다고 생각합니다.

조기 최적화를 피하는 것이 좋습니다. 예를 바탕으로 성능이 많은 문제가 될 것 같습니다. 실제로 성능 문제를보고 있습니까?

다른 팁

지금 당신은 두 번 추가를하고 있습니다. 나는 이것을 할 것이다 :

get{ 
        var newSpeed = CurrentSpeed + CarAcceleration;
        if( newSpeed >= MaxSpeed){
            return MaxSpeed;
        }
        else{
            return newSpeed;
        }
}

뒤늦게 보면이 코드의 더 깨끗한 버전은 다음과 같습니다.

get{ 
        var newSpeed = CurrentSpeed + CarAcceleration;
        if( newSpeed >= MaxSpeed){
            newSpeed = MaxSpeed;
        }

        return newSpeed;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top