0Pricing
C# Academy · 课时

C# 中的构造函数

了解如何使用 C# 中的构造函数初始化对象

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

构造函数简介

C# 中的构造函数

欢迎来到下一课!本课将介绍 C# 中的构造函数、它们的用途,以及如何使用它们初始化对象。让我们开始吧!

C# 中的构造函数 — 插图 1

什么是构造函数?

什么是构造函数?

构造函数是类中的特殊方法,在创建该类的对象时会自动调用。构造函数用于初始化对象数据或执行设置任务。

关键点:构造函数的名称与类名相同,并且没有返回类型。

定义构造函数

定义构造函数

要定义构造函数,请创建一个与类名相同的方法。示例:

提示:请使用构造函数,确保对象始终以有效状态创建。

using System;

class Car {
    public string Color;

    // Constructor
    public Car() {
        Color = "Red"; // Initialize the Color attribute
    }
}

class Program {
    static void Main() {
        Car myCar = new Car(); // Constructor is automatically called
        Console.WriteLine("Car color: " + myCar.Color); // Outputs: Car color: Red
    }
}

参数化构造函数

参数化构造函数

参数化构造函数允许您在创建对象时传入值,以初始化对象数据。示例:

关键点:请使用参数化构造函数,以灵活地初始化对象。

using System;

class Car {
    public string Color;

    // Parameterized Constructor
    public Car(string color) {
        Color = color; // Initialize Color with the passed value
    }
}

class Program {
    static void Main() {
        Car myCar = new Car("Blue"); // Pass "Blue" to the constructor
        Console.WriteLine("Car color: " + myCar.Color); // Outputs: Car color: Blue
    }
}

默认构造函数

默认构造函数

如果没有显式定义构造函数,C# 会提供一个默认构造函数,将字段初始化为其默认值。示例:

提示:当需要特定的初始化操作时,请定义自己的构造函数。

using System;

class Car {
    public string Color; // Default value is null
}

class Program {
    static void Main() {
        Car myCar = new Car(); // Default constructor is automatically called
        Console.WriteLine("Car color: " + (myCar.Color ?? "Not set")); // Outputs: Car color: Not set
    }
}

构造函数重载

构造函数重载

C# 支持构造函数重载,也就是说,一个类可以拥有多个参数列表不同的构造函数。示例:

关键点:构造函数重载让对象创建更加灵活。

using System;

class Car {
    public string Color;

    // Default Constructor
    public Car() {
        Color = "Red";
    }

    // Parameterized Constructor
    public Car(string color) {
        Color = color;
    }
}

class Program {
    static void Main() {
        Car defaultCar = new Car(); // Calls the default constructor
        Console.WriteLine("Default car color: " + defaultCar.Color); // Outputs: Default car color: Red

        Car customCar = new Car("Green"); // Calls the parameterized constructor
        Console.WriteLine("Custom car color: " + customCar.Color); // Outputs: Custom car color: Green
    }
}

练习挑战

练习挑战

创建一个名为 Book 的类,其中包含一个将标题设置为“Unknown”的默认构造函数,以及一个接受标题的参数化构造函数。请分别显示这两个构造函数创建的图书标题。示例:

using System;

class Book {
    public string Title;

    // Default Constructor
    public Book() {
        Title = "Unknown";
    }

    // Parameterized Constructor
    public Book(string title) {
        Title = title;
    }
}

class Program {
    static void Main() {
        Book defaultBook = new Book();
        Console.WriteLine("Default book title: " + defaultBook.Title); // Outputs: Default book title: Unknown

        Book customBook = new Book("C# Programming");
        Console.WriteLine("Custom book title: " + customBook.Title); // Outputs: Custom book title: C# Programming
    }
}

构造函数的最佳实践

构造函数的最佳实践

以下是使用构造函数时的一些最佳实践:

  • 保持构造函数简单,并专注于初始化。
  • 避免在构造函数中编写复杂逻辑,以确保代码清晰。
  • 使用构造函数重载,提高对象创建的灵活性。

提示:构造函数应确保对象在创建后立即具备完整功能。

做得很好!

做得很好!

恭喜您!您已经了解了构造函数是什么、如何定义构造函数,以及构造函数在对象初始化中的重要性。在下一课中,我们将探索继承的概念,并学习如何有效地复用代码。让我们继续编写代码吧!

C# 中的构造函数 — 插图 10

常见问题解答

「C# 中的构造函数」课时是免费的吗?

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

「C# 中的构造函数」这节课中我会学到什么?

了解如何使用 C# 中的构造函数初始化对象 你通过在浏览器中直接运行的动手代码来练习 C# Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

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

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

「C# 中的构造函数」课时需要多长时间?

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

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

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

此课程中的所有课时

  1. 什么是面向对象编程?
  2. 定义类和对象
  3. 理解属性和方法
  4. C# 中的构造函数
  5. 封装和访问修饰符
  6. 继承:复用代码
  7. 多态和方法重写
← 返回 C# Academy