C#でのカプセル化に関する一見単純な質問があります。C#で正しくカプセル化する方法Mainでは値を変更できません
私たちの学校の課題の1つでは、単純な2次元座標計算システムを作成し、ファイルを正しくカプセル化して、メインメソッドからポイントの位置や長さを編集することはできません。
私は2つのC#ファイル、すなわちラインとポイントを作成しました。 Program.csファイルが私たちに与えられたので、その問題を回避する必要があります。そのファイルのコードを変更することはできません。カプセル化するファイルは、Line.csファイルまたはPoint.csファイルです。そのため、Mainメソッドではポイントの位置をそこから編集することはできません。 p1.X = 0;
。
Program.csの
using System;
using coordinationSystem;
class Program
{
public static void Main()
{
Point p0 = new Point();
Point p1 = new Point(-10, -10);
Console.WriteLine("Point p0, position = (" + p0.X + ", " + p0.Y + ")");
Console.WriteLine("Point p1, position = (" + p1.X + ", " + p1.Y + ")");
Line theLine = new Line(p0, p1); //This is the correct line.
outdata("Print 1 of the line", theLine);
p1.X = 0; //This is the thing I'm trying to prevent...
outdata("Print 2 of the line", theLine);
Point startposition = theLine.Position();
startposition.Y = 5; //... same with this one
outdata("Print 3 of the line", theLine);
}
private static void outdata(string text, Line theLine)
{
double length = theLine.Length();
Point startPos = theLine.Position();
Console.WriteLine("\n" + text);
Console.WriteLine("==================================");
Console.WriteLine("Length = {0 :f4} units", length);
Console.WriteLine("Position = ({0},{1})", startPos.X, startPos.Y);
Console.WriteLine("----------------------------------");
}
}
namespace coordinationSystem
{
public class Point
{
private int x, y;
public Point() : this(0, 0) { } //Default-constructor.
public Point(int X, int Y) //Constructor.
{
this.X = X;
this.Y = Y;
}
public int X //Property for the X-coordinate.
{
get { return x; }
set
{
x = value;
}
}
public int Y //Property for the Y-coordinate.
{
get { return y; }
set
{
y = value;
}
}
}
}
Line.cs
using System;
namespace coordinationSystem
{
public class Line
{
private Point p0;
private Point p1;
public Line(Point p0, Point p1)
{
this.p0 = p0;
this.p1 = p1;
}
public double Length()
{
double pointLength = Math.Sqrt(Math.Pow(p0.X - p1.X, 2) + Math.Pow(p0.Y - p1.Y, 2));
return pointLength;
}
public Point Position()
{
return p0;
}
}
}
Point.cs: 以下は、私がこれまで書いてきたファイルをしています
私はおそらくget
とset
を使用すべきだという結論に達しましたが、MainのtheLine.Position();
がメソッドであるため(これは私が以前に書いたように、プログラムを編集するはずがないため) cs/Main)を実行します。
ここにアイデアはありますか?どんな助けでも大歓迎です!
EDIT:
これは(緩くスウェーデン語から翻訳)割り当てテキストです:Mainメソッドからのアクセスの問題を回避するために
Consider whether it is possible to sabotage the line if you have access to references to the lines both endpoints. What happens, for example, with the line if you would change the p0 or p1 in the Main method after you have created the line? What happens to the line when you move the starting position in the Main method? Modify the code in the class Line and Point so that the above problems are avoided.
「メイン」を変更するつもりがない場合は、どのようにして 'p1.X = 0'を「防止する」ことを望みますか?あなたはそれをコンパイルしたくない、あるいは他の何かをしたいですか? (Btw、今は自動的に実装されたプロパティについて学ぶ良い時間です...) –
あなたがLine.csではなくPoint.csにアクセスするとコメントした行。 – Florian
あなたはそうかもしれませんが、Line.csとPoint.csの両方を編集できるように変更する必要があります。私の悪い。 – tobulos1