close
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ObjectTest
{
class Rectangle
{
public double width, height;
public Rectangle(double w, double h)
{
width = w;
height = h;
}
public double area()
{
return width * height;
}
public void print()
{
Console.WriteLine("width=" + width + " height=" + height + " area()=" + area());
}
}
class Circle
{
public double r;
public Circle(double r)
{
this.r = r;
}
public double area()
{
return 3.14 * r * r;
}
public void print()
{
Console.WriteLine("r=" + r + " area()=" + area());
}
}
class Program
{
static void Main(string[] args)
{
Rectangle r = new Rectangle(5.0, 3.0);
r.print();
Rectangle r2 = new Rectangle(4.0, 6.0);
r2.print();
Circle c1 = new Circle(1.0);
c1.print();
Circle c2 = new Circle(2.0);
c2.print();
}
}
}
文章轉載:陳鍾誠 (2011年10月04日),(網頁標題) 矩形與圓形物件,(網站標題) 免費電子書:C# 程式設計,2011年10月04日,取自 http://cs0.wikidot.com/circleobject ,網頁修改第 1 版。
文章標籤
全站熱搜
留言列表