···
class Program
{
static void Main(string[] args)
{
RectangeClass rec = new RectangeClass(4, 5);
int mianji = rec.Mianji();
int zhouchang = rec.Zhouchang();
Console.WriteLine("{0} \n{1}", mianji, zhouchang);
rec.UpDate(5, 6);
RoundClass rou = new RoundClass(8);
double s1 = rou.GetRoundDate(1);
double s2 = rou.GetRoundDate(2);
Console.WriteLine("{0}\n{1}", s1, s2);
Console.ReadKey();
}
}
···
···
class RectangeClass
{
//长
private int length;
//宽
private int width;
//构造函数初始化 通过参数去改变
public RectangeClass(int length1, int width1)
{//实例化时必须传参
length = length1;
width = width1;
}
//矩形周长
public int Zhouchang()
{
int num = 0;
num = 2 * (length + width);
return num;
}
//面积
public int Mianji()
{
return length * width;
}
//改变矩形长宽的方法
public void UpDate(int _length, int _width)
{
length = _length;
width = _width;
}
}
···
···
class RoundClass
{
private double r;
public RoundClass(double R)
{//构造函数初始化
r = R;
}
public double GetRoundDate(int num)
{
double RoundNum = 0;
if (num == 1)
{
RoundNum = 2 * Math.PI * r;
}
else if (num == 2)
{
RoundNum = Math.PI * r * r;
}
else
{
RoundNum = 0;
}
return RoundNum;
}
}
···