我有哪裏我聲明一個類變量,但是當我在方法中使用他們的示例代碼中聲明的變量,值變爲零。請查找下面的代碼進一步的細節。如何初始化一個類
class RectangleExample
{
double i;
double j;
public void GetValues()
{
i = 2.5;
j = 3.5;
}
public double GetArea()
{
return i * j;
}
public void Display()
{
Console.WriteLine("Length: {0}", i);
Console.WriteLine("width: {0}", j);
Console.WriteLine("Area: {0}", GetArea());
}
}
class ExecuteRectangle
{
static void Main(string[] args)
{
RectangleExample obj1 = new RectangleExample();
obj1.Display();
Console.ReadKey();
}
}
分享你的代碼。 –
*「請找到下面的代碼」*找不到它......你把它藏在哪裏? –
您是否聽說過構造函數? :) – C1sc0