首頁  >  文章  >  後端開發  >  C#結構和類別

C#結構和類別

高洛峰
高洛峰原創
2017-02-08 14:39:061139瀏覽

       類別和結構是.NET Framework中的相同類型系統的兩種基本建構。兩者本質上都屬於資料結構,封裝這一組整體作為一個邏輯單位的資料和行為。資料和行為是該類別或結構的“成員”,它們包含各自的方法,屬性和事件等。

       結構

       結構為C#程式設計師用來定義自己的數值類型的最普遍的機制。結構比枚舉更強大,因為它提供方法,字段,操作符和訪問控制等。

       結構與類別很相似,都表示可以包含資料成員和函數成員的資料結構。但是,與類別不同,結構是一種值類型,並且不需要堆分配。結構類型的變數直接包含了該結構的數據,而類別類型的變數所包含的只是對相應數據的一個引用(被引用的數據稱為「物件」)。

        結構對於具有值語意的小型資料結構特別有用。複數,座標系中的點或字典中的“鍵-值”對都是結構的典型範例。這些資料結構的關鍵之處在於:他們只是少量資料成員,不要求使用繼承或引用標識,而且它們使用較方便(賦值時直接複製值而不是複製它的引用)。

       

        結構的聲明透過關鍵字struct來實現,聲明格式為:

        修飾             結構主體

          };

         結構宣告包含一組選用特性,並於接一個群組可選的結構修飾符,再跟關鍵字struct和一個用於命名結構的標識符,然後跟一個可選的結構接口規範,最後跟一個結構主體,根據需要後面還可以跟一個分號。

        結構聲明可以根據需要包含結構修飾符:new,public,protected,internal,private

       結構的使用


       結構的使用

       結構的使用

       結構的使用


       結構的使用

       結構的使用


       結構的使用

       結構的使用

       結構的使用

初始化實例字段也是錯誤的。初始化結構成員可透過兩種形式來完成:一是使用參數化建構函數,二是在宣告結構後分別存取成員。對於任何私有成員或以其他方式設定為不可存取的成員,只能在建構函式中進行初始化。

        如果使用new運算子建立結構對象,則會建立該結構對象,並呼叫適當的建構子。與類別不同的是,結構的實例化可以不使用new運算子。在此情況下不存在建構函數的調用,因而可以提高分配效率。但是,在初始化所有欄位之前,欄位將保持未賦值狀態且物件不可用。

🎜        當結構包含引用類型為成員時,必須明確地呼叫呼叫該成員的預設建構函數,否則該成員將保持未賦值狀態且結構不可用。 🎜🎜例題,建立一個結構,分析結構的使用🎜🎜🎜🎜🎜        類別🎜🎜        類別是一種資料結構,它可以封裝資料成員(常數運算符,實例建構函數,靜態建構子和析構函數)和其他的類別(巢狀類型)。類別是創建物件的模板。 C#的一切類型都是類,所有的語句都必須位於類別內,不存在任何處於類別外的語句。因此,類別是C#語言的核心和基本構成模組。類別類型支援繼承,繼承是一種機制,可使衍生類別對基底類別進行擴展和專用化。 🎜🎜        類別聲明中指定的基底類別可以是建構類別類型。基類本身不能是類型形參,但在其作用域中可以包含類型形參。 🎜🎜        在OOP方法中,類別是對現實世界的一種高度抽象與概括,而物件則是類別的實例,物件必須具有其屬類別的共同特徵與行為規則,當然一個物件還可以具有其屬類未曾規定的特徵和行為規則。這一點和現實生活是相同的,這樣的模擬和抽像比較符合人們的思維習慣,這也正是OOP方法具有強大生命力,能夠獲得越來越多的軟體工作者歡迎並得到眾多電腦開發商支持的一個基本原因。 🎜🎜🎜🎜🎜       總之,從程式設計者的角度觀察,類別是資料模式與若干個程式過程,經封裝而形成的整體,是使用資訊科技對現實世界的一種模擬與抽象。而物件則是類別的一個實例,從程式設計語言來說,物件可以被理解為一個類別賦值的結果。物件是OOP方法中組成程式的一種構件。 🎜

The declaration of a class in C# is instantiated through the class keyword. The format is:

Modifier class class name: base class or interface

{

Class body

}

Among them, "modifier" and ": base class or interface" are optional. The modifier of a class can be one of the following or a combination thereof (the same modifier is not allowed to appear multiple times in the class declaration ()).

(1)new: Only allowed to be used when declaring a nested class, indicating that the class hides members inherited from the base class and with the same name as the base class

(2 )public: Indicates that access to the class is not restricted

(3)internal: Only the class in which it belongs can access

(4) Private: Only applications or libraries in .NET can access it Access

(5)Abstract: Abstract class, creation of instances of the class is not allowed

(6)Sealed: Sealed class, inheritance is not allowed


Class inheritance statement: C# language only supports single inheritance

(1), constructor and destructor

C# provides a better mechanism to enhance program security sex. The C# compiler has a strict type safety check function, which can find almost all syntax problems in the program. However, just because the program passes the compilation check does not mean that the error no longer exists.

The C# language fully considers the occurrence of program errors and solves them well, that is, placing the object initialization work in the constructor and the cleanup work in the destructor. When an object is created, the constructor is executed automatically. When the object dies, the destructor is executed automatically.


The name of the constructor cannot be chosen casually. It must be recognized by the compiler before it can be automatically executed. Its naming method is simple and reasonable: let the constructor have the same name as the class. In addition to the name, another special feature of the constructor is that it has no return value type, which is different from the function whose return value type is void.


The destructor is a method member that destroys an instance of a class. The destructor cannot have parameters, cannot have any modifiers and cannot be called. The purpose of the destructor and the constructor is different, so the destructor is prefixed with "~" to show the difference.

Although the constructor and destructor are formally simpler functions in a class. But their use is by no means as simple as it seems, so using constructors and destructors flexibly and correctly can help users better understand the CLR's memory management mechanism and better manage resources in the system.


(2), Classes and Objects

A class is a collection of objects with the same attributes and services. It provides a unified abstract description for all objects belonging to this class, which includes two main parts: properties and services. In object-oriented programming languages, a class is an independent program unit, which has a class name. The class name includes two main parts: attribute description and service description.


An object is an entity used to describe objective things in the system and is a basic unit that constitutes the system. An object consists of a set of properties and a set of services that operate on the set of properties. From a more abstract perspective, an object is an abstraction of something in the problem domain or implementation domain. It reflects the information that the thing needs to save and the role it plays in the system. It is a set of attributes and the right to perform operations on these attributes. A wrapper for a set of services that operates. The objective world is composed of objects and the connections between objects.

The relationship between a class and an object is like the relationship between a mold and a casting. The result of instantiation of a class is an object. The abstraction of a type of object is a class. A class describes a group of objects with the same characteristics (properties) and the same behavior (methods).

For more articles related to C# structures and classes, please pay attention to the PHP Chinese website!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn