实现与测试
实现 Rectangle、Circle 和 Triangle;添加基本输入保护;在不使用框架的情况下编写小型临时测试。
实现与测试 是 CoddyKit 上的免费 C# Academy 课时。 这是第 2 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 C# Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 C# Academy 课程共包含 3 节课。
实现计划
目标:实现形状并验证它们。
- 实现 Rectangle、Circle、Triangle
- 防止无效输入
- 添加一个小型 Assert 辅助类
- 在 Main 中执行快速检查
矩形与圆
在构造函数中添加简单的校验,确保尺寸为正数。让公式保持简短清晰。
using System;
public interface IShape
{
double Area();
double Perimeter();
}
public sealed class Rectangle : IShape
{
private readonly double _w, _h;
public Rectangle(double w, double h)
{
if (w <= 0 || h <= 0) throw new ArgumentOutOfRangeException("w/h must be > 0");
_w = w; _h = h;
}
public double Area() { return _w * _h; }
public double Perimeter() { return 2 * (_w + _h); }
}
public sealed class Circle : IShape
{
private readonly double _r;
public Circle(double r)
{
if (r <= 0) throw new ArgumentOutOfRangeException("r must be > 0");
_r = r;
}
public double Area() { return 3.14159 * _r * _r; }
public double Perimeter() { return 2 * 3.14159 * _r; }
}
public class Program
{
public static void Main(string[] args)
{
IShape a = new Rectangle(3, 4);
IShape b = new Circle(2);
Console.WriteLine("Rect A=" + a.Area() + " P=" + a.Perimeter());
Console.WriteLine("Circ A=" + b.Area() + " P=" + b.Perimeter());
}
}
带校验的三角形
验证边长(三角形不等式)。使用已知公式,并让数字易于阅读。
using System;
public interface IShape
{
double Area();
double Perimeter();
}
public sealed class Triangle : IShape
{
private readonly double _a, _b, _c;
public Triangle(double a, double b, double c)
{
if (a <= 0 || b <= 0 || c <= 0) throw new ArgumentOutOfRangeException("sides must be > 0");
// triangle inequality: each side < sum of other two
if (a >= b + c || b >= a + c || c >= a + b) throw new ArgumentException("invalid triangle");
_a = a; _b = b; _c = c;
}
public double Perimeter() { return _a + _b + _c; }
// Herons formula for area
public double Area()
{
double s = (_a + _b + _c) / 2.0;
double value = s * (s - _a) * (s - _b) * (s - _c);
return Math.Sqrt(value);
}
}
public class Program
{
public static void Main(string[] args)
{
IShape t = new Triangle(3, 4, 5);
Console.WriteLine("Tri A=" + t.Area() + " P=" + t.Perimeter());
}
}
临时 Assert 辅助类
创建一个小型 Assert 类。使用适用于双精度数的容差比较预期值与实际值。
using System;
public static class Assert
{
// Approx compare for doubles (tolerance)
public static void AreClose(double expected, double actual, double eps, string msg)
{
if (Math.Abs(expected - actual) > eps)
{
throw new Exception("Assert failed: " + msg + " expected=" + expected + " actual=" + actual);
}
Console.WriteLine("OK: " + msg);
}
}
public interface IShape
{
double Area();
double Perimeter();
}
public sealed class Rectangle : IShape
{
private readonly double _w, _h;
public Rectangle(double w, double h) { if (w <= 0 || h <= 0) throw new ArgumentOutOfRangeException(); _w = w; _h = h; }
public double Area() { return _w * _h; }
public double Perimeter() { return 2 * (_w + _h); }
}
public class Program
{
public static void Main(string[] args)
{
Rectangle r = new Rectangle(3, 4);
Assert.AreClose(12.0, r.Area(), 0.0001, "rectangle area");
Assert.AreClose(14.0, r.Perimeter(), 0.0001, "rectangle perimeter");
}
}
运行快速测试
在 Main 中汇总几个测试。让预期结果保持简单易读。
using System;
public static class Assert
{
public static void AreClose(double expected, double actual, double eps, string msg)
{
if (Math.Abs(expected - actual) > eps)
throw new Exception("Assert failed: " + msg + " expected=" + expected + " actual=" + actual);
Console.WriteLine("OK: " + msg);
}
}
public interface IShape
{
double Area();
double Perimeter();
}
public sealed class Circle : IShape
{
private readonly double _r;
public Circle(double r) { if (r <= 0) throw new ArgumentOutOfRangeException(); _r = r; }
public double Area() { return 3.14159 * _r * _r; }
public double Perimeter() { return 2 * 3.14159 * _r; }
}
public sealed class Triangle : IShape
{
private readonly double _a, _b, _c;
public Triangle(double a, double b, double c)
{
if (a <= 0 || b <= 0 || c <= 0) throw new ArgumentOutOfRangeException();
if (a >= b + c || b >= a + c || c >= a + b) throw new ArgumentException("invalid triangle");
_a = a; _b = b; _c = c;
}
public double Perimeter() { return _a + _b + _c; }
public double Area()
{
double s = (_a + _b + _c) / 2.0;
return Math.Sqrt(s * (s - _a) * (s - _b) * (s - _c));
}
}
public class Program
{
public static void Main(string[] args)
{
double eps = 0.001;
Circle c = new Circle(2);
Assert.AreClose(12.56636, c.Area(), eps, "circle area r=2");
Assert.AreClose(12.56636, c.Perimeter(), eps, "circle peri r=2");
Triangle t = new Triangle(3, 4, 5);
Assert.AreClose(12.0, t.Perimeter(), eps, "triangle perimeter 3-4-5");
Assert.AreClose(6.0, t.Area(), eps, "triangle area 3-4-5");
}
}
实现提示
检查清单:
- 在构造函数中验证输入。
- 让公式保持简短;使用局部常量表示圆周率。
- 为临时测试创建一个小型 Assert。
- 为每种形状测试 Area 和 Perimeter。
临时测试思路
回顾
回顾:使用清晰的校验实现形状,然后在 Main 中通过最小化的 Assert 辅助类进行验证。让测试保持简短且具有确定性。
常见问题解答
「实现与测试」课时是免费的吗?
是的 — 「实现与测试」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 C# Academy 课程的其余内容,请升级到 CoddyKit PRO。 C# Academy 课程共包含 3 节课。
「实现与测试」这节课中我会学到什么?
实现 Rectangle、Circle 和 Triangle;添加基本输入保护;在不使用框架的情况下编写小型临时测试。 你通过在浏览器中直接运行的动手代码来练习 C# Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 C# Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 C# Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 3 节。
「实现与测试」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 C# Academy 课中编写并运行代码吗?
能。每节 C# Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。