나는 한 가지 오류가있어 내 책에서 답변을 검색하고이 특정 주제에 대한 자습서를 보았습니다. 큰 차이는 재산이없는 동안 당신의 ObjectCount()
방법은 static
방법이다왜 내가 이해할 수 없는지 CS0120
class Program
{
private static Point another;
static void Main(string[] args)
{
Point origin = new Point(1366, 768);
Point bottomRight = another;
double distance = origin.DistanceTo(bottomRight);
Console.WriteLine("Distance is: {0}", distance);
Console.WriteLine("Number of Point objects: {0}", Point.ObjectCount());
}
}
class Point {
private int x, y;
private int objectCount = 0;
public Point()
{
this.x = -1;
this.y = -1;
objectCount++;
}
public Point(int x, int y)
{
this.x = x;
this.y = y;
objectCount++;
}
public double DistanceTo(Point other)
{
int xDiff = this.x - other.x;
int yDiff = this.y - other.y;
double distance = Math.Sqrt((xDiff * xDiff) + (yDiff * yDiff));
return distance;
}
public static int ObjectCount()
{
**return objectCount;**
}
}
커뮤니티에 오신걸 환영합니다. 다른 사람들에게 도움을 받으려면 좋은 질문을 게시해야합니다. 코드에서 불필요한 부분을 제거하고 단순화하십시오. 물론 대신 공백을 넣지 마십시오. 다른 사람들이 문제에 시간을 보내길 원한다면 먼저 관심을 갖고 시간을 보내야합니다 –