「C#入門」アクセス修飾子publicを利用するサンプル
サンプルコード
using System;
namespace RectangleApplication
{
class Rectangle
{
//メンバー
public double length;
public double width;
public double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("length: {0}", length);
Console.WriteLine("width: {0}", width);
Console.WriteLine("area: {0}", GetArea());
}
}
class ExecuteRectangle
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.length = 4.5;
r.width = 3.5;
r.Display();
Console.ReadLine();
}
}
}
実行結果
length: 4.5
width: 3.5
area: 15.75