抽象类
Coddy 面向 C++ 开发者的 C# 课程的第 39 课,共 48 课。
C++ 使用纯虚方法构建抽象基类;C# 使用抽象声明和重写实现。通过基类指针删除对象时,C++ 多态对象的生命周期需要合适的析构函数。C# 托管引用采用不同的销毁模型。
抽象类可以定义共享结构,并要求派生类实现相应行为。抽象类不能直接实例化。抽象方法在基类中没有函数体,派生的具体类必须重写它们。多态集合让调用方无需检查每个具体类的类型,就能请求计算面积。
C++ 参考
#include <algorithm>
#include <array>
#include <cctype>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
using namespace std;
struct Shape { virtual double area() const=0; virtual ~Shape()=default; };
struct Triangle: Shape { int b,h; Triangle(int b,int h):b(b),h(h) {} double area() const override { return b*h/2.0; } };
int main() {
Triangle triangle(3,2); Shape& shape=triangle; cout << fixed << setprecision(1) << shape.area() << "\n";
}
C# 支持性声明
abstract class Named { public abstract string Describe(); }
class NamedCode : Named { private string code; public NamedCode(string code) { this.code = code; } public override string Describe() { return "code:" + code; } }Main 中的 C# 语句
Named item = new NamedCode("A");
Console.WriteLine(item.Describe());这些是重点教学片段。挑战文件包含完整的可执行封装;上面的代码片段展示了相关声明和语句。
参考输出:
3.0C# 输出:
code:A挑战
简单实现继承自抽象类 Shape 的 Rectangle 和 Triangle。Rectangle 的面积为 width * height。Triangle 的面积为 base * altitude / 2.0。通过 Shape 引用返回总和。尺寸均为非负整数。
编辑未锁定的 C# 文件。可见的锁定文件 Main.cs 会读取所述各行,调用 Exercise.Solve,验证输入未被修改,并在末尾输出一个换行符。保留已声明的 API。不要添加第二个 Main,也不要输出诊断信息。
锁定的启动器还会独立测试导出的辅助函数或类 API,而不只是测试 Solve 包装器。
亲手试试
using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
public class Program {
static int I(string text) { return int.Parse(text,CultureInfo.InvariantCulture); }
static int[] Ints(string text) { return text.Length==0 ? new int[0] : text.Split(',').Select(I).ToArray(); }
static string[] Words(string text) { return text.Length==0 ? new string[0] : text.Split(','); }
static string Snapshot(object value) { if (value==null) return "<null>"; if (value is string) return (string)value; var sequence=value as System.Collections.IEnumerable; if (sequence!=null) { var items=new List<string>(); foreach (object item in sequence) items.Add(Snapshot(item)); return string.Join("|",items); } return Convert.ToString(value,CultureInfo.InvariantCulture); }
public static void Main() {
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
var rows = new List<string>(); string line; while ((line=Console.ReadLine()) != null) rows.Add(line);
string[] input=rows.ToArray();
var a0 = I(input[0]); string before0=Snapshot(a0);
var a1 = I(input[1]); string before1=Snapshot(a1);
var a2 = I(input[2]); string before2=Snapshot(a2);
var a3 = I(input[3]); string before3=Snapshot(a3);
Shape sh1 = new Rectangle(2,3); Shape sh2 = new Triangle(1,1); if (sh1.Area() != 6 || sh2.Area() != 0.5) throw new Exception("Shape API");
var result = Exercise.Solve(a0,a1,a2,a3);
if (Snapshot(a0) != before0) throw new Exception("Input mutation");
if (Snapshot(a1) != before1) throw new Exception("Input mutation");
if (Snapshot(a2) != before2) throw new Exception("Input mutation");
if (Snapshot(a3) != before3) throw new Exception("Input mutation");
Console.WriteLine(result.ToString("0.0", CultureInfo.InvariantCulture));
}
}
这节课包含一个小测验。开始学习即可作答,并记录你的进度。
面向 C++ 开发者的 C#中的所有课
1从这里开始
运行你的第一个 C# 程序自己练习: C# 在线编译器