0Pricing
C# Academy · 课时

构造函数与静态成员

使用构造函数(包括重载和链式调用)创建对象,并使用静态字段/方法保存所有实例共享的数据。

构造函数与静态成员 是 CoddyKit 上的免费 C# Academy 课时。 这是第 2 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 C# Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 C# Academy 课程共包含 3 节课。

构造函数和 static:概览

目标:安全地构造对象,并在实例之间共享数据。

  • 构造函数设置所需状态
  • 重载与 this(...) 链式调用
  • 所有对象共享的 static fields/方法

基本构造函数

调用 new 时会运行构造函数,并初始化对象。

using System;

// Constructor sets required data on creation
public class User
{
  public string Name { get; private set; }
  public int Age { get; private set; }

  public User(string name, int age)
  {
    Name = name;
    Age = age;
  }
}

public class Program
{
  public static void Main(string[] args)
  {
    User u = new User("Ada", 28);
    Console.WriteLine(u.Name + " (" + u.Age + ")");
  }
}

重载与链式调用

使用构造函数重载提供便利,并使用 this(...) 复用逻辑。

using System;

// Overloads let you offer defaults; chain with this(...)
public class Point2D
{
  public int X { get; private set; }
  public int Y { get; private set; }

  public Point2D(int x, int y)
  {
    X = x; Y = y;
  }

  public Point2D(int xy) : this(xy, xy) { } // calls the main constructor
}

public class Program
{
  public static void Main(string[] args)
  {
    Point2D p1 = new Point2D(3, 4);
    Point2D p2 = new Point2D(5); // becomes (5,5)
    Console.WriteLine("p1: " + p1.X + "," + p1.Y);
    Console.WriteLine("p2: " + p2.X + "," + p2.Y);
  }
}

static 成员

static 成员由所有实例共享。通过类型名称调用 static 方法。

using System;

// Static members live on the type (one copy shared)
public class Counter
{
  private static int _created = 0;   // shared count
  public int Id { get; private set; }

  public Counter()
  {
    _created = _created + 1;
    Id = _created;
  }

  public static int CreatedCount()
  {
    return _created;
  }
}

public class Program
{
  public static void Main(string[] args)
  {
    Counter a = new Counter();
    Counter b = new Counter();
    Console.WriteLine("a.Id=" + a.Id + ", b.Id=" + b.Id);
    Console.WriteLine("Total created = " + Counter.CreatedCount());
  }
}

static 构造函数与常量

static 构造函数每种类型只运行一次。使用 const 表示编译时字面量,使用 static readonly 表示在运行时设置的值。

using System;

public class AppConfig
{
  public const string AppName = "Demo";      // compile-time constant
  public static readonly DateTime StartedAt; // runtime-initialized once

  // Static constructor: runs once before first use of the type
  static AppConfig()
  {
    StartedAt = DateTime.UtcNow;
  }

  public static string Info()
  {
    return AppName + " started at " + StartedAt.ToString("u");
  }
}

public class Program
{
  public static void Main(string[] args)
  {
    Console.WriteLine(AppConfig.Info());
    Console.WriteLine(AppConfig.Info()); // static ctor ran only once
  }
}

提示

提示:

  • 保持构造函数简短;尽早验证输入。
  • 使用重载提供常见默认值,并链接到一个主构造函数。
  • 仅当数据确实属于该类型时才使用 static(例如计数器和辅助函数)。

static 成员的概念

快速检查:关于 static 成员,哪项说法是正确的?

回顾

回顾:使用构造函数设置所需状态,通过重载和链式调用提供便利,并添加由所有对象共享的数据或辅助函数的 static 成员。

常见问题解答

「构造函数与静态成员」课时是免费的吗?

是的 — 「构造函数与静态成员」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 C# Academy 课程的其余内容,请升级到 CoddyKit PRO。 C# Academy 课程共包含 3 节课。

「构造函数与静态成员」这节课中我会学到什么?

使用构造函数(包括重载和链式调用)创建对象,并使用静态字段/方法保存所有实例共享的数据。 你通过在浏览器中直接运行的动手代码来练习 C# Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 C# Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 C# Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 3 节。

「构造函数与静态成员」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 C# Academy 课中编写并运行代码吗?

能。每节 C# Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 字段、自动属性与对象初始化器
  2. 构造函数与静态成员
  3. 封装与不变量
← 返回 C# Academy