用c#编写程序定义圆类 5
包含数据成员和方法成员:数字常量pi=3.14159两个公用字段xcenter,ycenter一个半径字段radius定义至少1个及以上的构造函数创建圆对象定义一个set...
包含数据成员和方法成员:
数字常量pi=3.14159
两个公用字段xcenter,ycenter
一个半径字段radius
定义至少1个及以上的构造函数创建圆对象
定义一个setcenter设置圆的中心点
定义一个方法返回圆的半径
定义一个area计算圆的面积
重载tosting方法返回圆的信息
重载运算符+,计算两个圆相加的结果,+的规则为:圆点坐标相加,半径相加
定义析构函数,输出“圆对象释放了” 展开
数字常量pi=3.14159
两个公用字段xcenter,ycenter
一个半径字段radius
定义至少1个及以上的构造函数创建圆对象
定义一个setcenter设置圆的中心点
定义一个方法返回圆的半径
定义一个area计算圆的面积
重载tosting方法返回圆的信息
重载运算符+,计算两个圆相加的结果,+的规则为:圆点坐标相加,半径相加
定义析构函数,输出“圆对象释放了” 展开
1个回答
展开全部
class CCircle:IDisposable {
/// <summary>
/// 常量 Pi
/// </summary>
const double pi = 3.14159;
/// <summary>
/// 公共字段
/// </summary>
public double xcenter;
public double ycenter;
/// <summary>
/// 私有字段半径
/// </summary>
double _radius;
/// <summary>
/// 构造函数1
/// </summary>
/// <param name="radius">半径</param>
public CCircle(double radius) {
this._radius = radius;
}
/// <summary>
/// 构造函数2
/// </summary>
/// <param name="radius">半径</param>
/// <param name="x">x坐标</param>
/// <param name="y">y坐标</param>
public CCircle(double radius, double x, double y) {
this.xcenter = x;
this.ycenter = y;
this._radius = radius;
}
/// <summary>
/// 设置中心点
/// </summary>
/// <param name="x">x坐标</param>
/// <param name="y">y坐标</param>
public void SetCenter(double x, double y) {
this.xcenter = x;
this.ycenter = y;
}
/// <summary>
/// 返回半径
/// </summary>
/// <returns>半径长</returns>
public double GetRadius() {
return this._radius;
}
/// <summary>
/// 计算面积
/// </summary>
/// <returns>结果</returns>
public double GetArea() {
return pi * (_radius * _radius);
}
/// <summary>
/// 重载tostring方法输出园信息
/// </summary>
/// <returns></returns>
public override string ToString() {
return "xcenter:" + xcenter.ToString()
+ " ycenter:" + ycenter.ToString()
+ " radius:" + _radius.ToString();
}
/// <summary>
/// 运算符+号重载
/// </summary>
/// <param name="obj1"></param>
/// <param name="obj2"></param>
/// <returns></returns>
public static CCircle operator +(CCircle obj1, CCircle obj2){
if(obj1 == null || obj2 == null){
throw new ArgumentNullException();
}
CCircle ret = new CCircle(obj1.GetRadius() + obj2.GetRadius());
ret.xcenter = obj1.xcenter + obj2.xcenter;
ret.ycenter = obj1.ycenter + obj2.ycenter;
return ret;
}
/// <summary>
/// 析构函数
/// </summary>
public void Dispose() {
Console.WriteLine("园对象释放了");
}
}
class Program{
static void Main(string[] args) {
CCircle circleObj1 = new CCircle(10, 1, 2);
CCircle circleObj2 = new CCircle(5);
circleObj2.SetCenter(10, 12);
Console.WriteLine("circle 1 radius" + circleObj1.GetRadius());
Console.WriteLine("circle 1 area" + circleObj1.GetArea());
Console.WriteLine("circle 1 info:" + circleObj1.ToString());
Console.WriteLine("circle 2 info:" + circleObj2.ToString());
Console.WriteLine("Add circle1 to circle2 to get a new cicle:" + (circleObj1 + circleObj2).ToString());
circleObj1.Dispose();
circleObj2.Dispose();
Console.Read();
}
}
/// <summary>
/// 常量 Pi
/// </summary>
const double pi = 3.14159;
/// <summary>
/// 公共字段
/// </summary>
public double xcenter;
public double ycenter;
/// <summary>
/// 私有字段半径
/// </summary>
double _radius;
/// <summary>
/// 构造函数1
/// </summary>
/// <param name="radius">半径</param>
public CCircle(double radius) {
this._radius = radius;
}
/// <summary>
/// 构造函数2
/// </summary>
/// <param name="radius">半径</param>
/// <param name="x">x坐标</param>
/// <param name="y">y坐标</param>
public CCircle(double radius, double x, double y) {
this.xcenter = x;
this.ycenter = y;
this._radius = radius;
}
/// <summary>
/// 设置中心点
/// </summary>
/// <param name="x">x坐标</param>
/// <param name="y">y坐标</param>
public void SetCenter(double x, double y) {
this.xcenter = x;
this.ycenter = y;
}
/// <summary>
/// 返回半径
/// </summary>
/// <returns>半径长</returns>
public double GetRadius() {
return this._radius;
}
/// <summary>
/// 计算面积
/// </summary>
/// <returns>结果</returns>
public double GetArea() {
return pi * (_radius * _radius);
}
/// <summary>
/// 重载tostring方法输出园信息
/// </summary>
/// <returns></returns>
public override string ToString() {
return "xcenter:" + xcenter.ToString()
+ " ycenter:" + ycenter.ToString()
+ " radius:" + _radius.ToString();
}
/// <summary>
/// 运算符+号重载
/// </summary>
/// <param name="obj1"></param>
/// <param name="obj2"></param>
/// <returns></returns>
public static CCircle operator +(CCircle obj1, CCircle obj2){
if(obj1 == null || obj2 == null){
throw new ArgumentNullException();
}
CCircle ret = new CCircle(obj1.GetRadius() + obj2.GetRadius());
ret.xcenter = obj1.xcenter + obj2.xcenter;
ret.ycenter = obj1.ycenter + obj2.ycenter;
return ret;
}
/// <summary>
/// 析构函数
/// </summary>
public void Dispose() {
Console.WriteLine("园对象释放了");
}
}
class Program{
static void Main(string[] args) {
CCircle circleObj1 = new CCircle(10, 1, 2);
CCircle circleObj2 = new CCircle(5);
circleObj2.SetCenter(10, 12);
Console.WriteLine("circle 1 radius" + circleObj1.GetRadius());
Console.WriteLine("circle 1 area" + circleObj1.GetArea());
Console.WriteLine("circle 1 info:" + circleObj1.ToString());
Console.WriteLine("circle 2 info:" + circleObj2.ToString());
Console.WriteLine("Add circle1 to circle2 to get a new cicle:" + (circleObj1 + circleObj2).ToString());
circleObj1.Dispose();
circleObj2.Dispose();
Console.Read();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询