C# 提供属性、事件、委托等特性,让面向对象开发更简洁。

本节要点

  • 属性使用 get/set 访问器。
  • virtual 和 override 支持多态。
  • interface 定义契约,类可以实现多个接口。

代码示例

public interface INotifier
{
    void Send(string message);
}

public class EmailNotifier : INotifier
{
    public string To { get; init; } = "";

    public void Send(string message)
    {
        Console.WriteLine($"发送到 {To}:{message}");
    }
}

INotifier notifier = new EmailNotifier { To = "[email protected]" };
notifier.Send("欢迎注册");