class(C# 参考)class (C# Reference)

使用 class 关键字声明类,如下例中所示:Classes are declared using the keyword class, as shown in the following example:

class TestClass
{
    // Methods, properties, fields, events, delegates
    // and nested classes go here.
}

备注Remarks

在 C# 中仅允许单一继承。Only single inheritance is allowed in C#. 也就是说,一个类仅能从一个基类继承实现。In other words, a class can inherit implementation from one base class only. 但是,一个类可实现多个接口。However, a class can implement more than one interface. 下表显示类继承和接口实现的一些示例:The following table shows examples of class inheritance and interface implementation:

继承Inheritance 示例Example
None class ClassA { }
SingleSingle class DerivedClass: BaseClass { }
无,实现两个接口None, implements two interfaces class ImplClass: IFace1, IFace2 { }
单一,实现一个接口Single, implements one interface class ImplDerivedClass: BaseClass, IFace1 { }

直接在命名空间中声明的、未嵌套在其它类中的类,可以是公共内部Classes that you declare directly within a namespace, not nested within other classes, can be either public or internal. 默认情况下类为 internalClasses are internal by default.

类成员(包括嵌套的类)可以是 publicprotected internalprotectedinternalprivateprivate protectedClass members, including nested classes, can be public, protected internal, protected, internal, private, or private protected. 默认情况下成员为 privateMembers are private by default.

有关详细信息,请参阅访问修饰符For more information, see Access Modifiers.

可以声明具有类型参数的泛型类。You can declare generic classes that have type parameters. 有关更多信息,请参见泛型类For more information, see Generic Classes.

一个类可包含下列成员的声明:A class can contain declarations of the following members:

示例Example

下面的示例说明如何声明类字段、构造函数和方法。The following example demonstrates declaring class fields, constructors, and methods. 该示例还说明如何实例化对象及如何打印实例数据。It also demonstrates object instantiation and printing instance data. 本例声明了两个类。In this example, two classes are declared. 第一个类 Child 包含两个私有字段(nameage)、两个公共构造函数和一个公共方法。The first class, Child, contains two private fields (name and age), two public constructors and one public method. 第二个类 StringTest 用于包含 MainThe second class, StringTest, is used to contain Main.

class Child
{
    private int age;
    private string name;

    // Default constructor:
    public Child()
    {
        name = "N/A";
    }

    // Constructor:
    public Child(string name, int age)
    {
        this.name = name;
        this.age = age;
    }

    // Printing method:
    public void PrintChild()
    {
        Console.WriteLine("{0}, {1} years old.", name, age);
    }
}

class StringTest
{
    static void Main()
    {
        // Create objects by using the new operator:
        Child child1 = new Child("Craig", 11);
        Child child2 = new Child("Sally", 10);

        // Create an object using the default constructor:
        Child child3 = new Child();

        // Display results:
        Console.Write("Child #1: ");
        child1.PrintChild();
        Console.Write("Child #2: ");
        child2.PrintChild();
        Console.Write("Child #3: ");
        child3.PrintChild();
    }
}
/* Output:
    Child #1: Craig, 11 years old.
    Child #2: Sally, 10 years old.
    Child #3: N/A, 0 years old.
*/

注释Comments

注意:在上例中,私有字段(nameage)只能通过 Child 类的公共方法访问。Notice that in the previous example the private fields (name and age) can only be accessed through the public method of the Child class. 例如,不能在 Main 方法中使用如下语句打印 Child 的名称:For example, you cannot print the child's name, from the Main method, using a statement like this:

Console.Write(child1.name);   // Error

只有当 Main 是类的成员时,才能从 Main 访问 Child 的私有成员。Accessing private members of Child from Main would only be possible if Main were a member of the class.

类中不具有访问修饰符的已声明类型默认为 private,因此如果已删除关键字,则此示例中的数据成员将仍为 privateTypes declared inside a class without an access modifier default to private, so the data members in this example would still be private if the keyword were removed.

最后要注意的是,默认情况下,对于使用默认构造函数 (child3) 创建的对象,age 字段初始化为零。Finally, notice that for the object created using the default constructor (child3), the age field was initialized to zero by default.

C# 语言规范C# language specification

有关详细信息,请参阅 C# 语言规范For more information, see the C# Language Specification. 该语言规范是 C# 语法和用法的权威资料。The language specification is the definitive source for C# syntax and usage.

请参阅See also