類別 (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
NoneNone class ClassA { }
SingleSingle class DerivedClass : BaseClass { }
無,實作兩個介面None, implements two interfaces class ImplClass : IFace1, IFace2 { }
單一,實作一個介面Single, implements one interface class ImplDerivedClass : BaseClass, IFace1 { }

直接在命名空間內宣告的類別 (未巢狀在其他類別內) 可以是 publicinternalClasses 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

請注意,在上述範例中,只能透過 Child 類別的公用方法來存取私用欄位 (nameage)。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 方法列印子系的名稱︰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 的 private 成員。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 parameterless 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