「DemoSquare という名前のプログラムを作成します。このプログラムは、1 ~ 10 の値を持つ辺を持つ 10 個の Square オブジェクトの配列を開始し、各正方形の値を表示します。Square クラスには、辺の面積と長さのフィールドが含まれています。面積と辺の長さのパラメータを必要とするコンストラクタ. コンストラクタはそのパラメータを正方形の辺の長さに割り当て, 面積フィールドを計算するプライベートメソッドを呼び出します. また, 正方形の辺を取得するための読み取り専用プロパティを含めます.そしてエリア。」
読み取り専用の割り当てのために領域を計算するためのプライベートメソッドを取得できないため、これはトリックの質問だと思いますが、ここに私のコードがあります:
class demoSquares
{
static void Main(string[] args)
{
Square[] squares = new Square[10];//Declares the array of the object type squares
for (int i = 0; i < 10; i++)
{
//Console.WriteLine("Enter the length");
//double temp = Convert.ToDouble(Console.ReadLine());
squares[i] = new Square(i+1);//Initializes the objects in the array
}
for (int i = 0; i < 10; i++)
{
Console.WriteLine(squares[i]);
}//end for loop, prints the squares
}//end main
}//end class
これは Square クラスです。
public class Square
{
readonly double length;
readonly double area;
public Square(double lengths)//Constructor
{
length = lengths;
area = computeArea();
}
private double computeArea()//getmethod
{
double areaCalc = length * length;
return areaCalc;
}
}