C# 第四次作業

來源:互聯網
上載者:User

標籤:

這節課老師首先介紹了structs 和class 的區別

1. Structs are value types.
2. Structs can be instantiated without using a new operator.Structs can declare constructors, but they must take parameters, can not declare default constructor-compiler will provide one & no Destructor.
3. A struct cannot inherit from another struct or class, and it cannot be the base of a class. It only inherit from the base class Object. A struct can implement interfaces.
4. Within a struct declaration, fields cannot be initialized unless they are declared as const or static.

定義strcts的格式如下▼

然後我自己試了一下定義struct▼

然後老師為我們講解了interface:

1. An interface is a contract that guarantees to a client how a class or struct will behave.

2. When a class implements an interface, it tells any potential client “I guarantee I’ll support all the methods, properties, events, and indexers of the named interface.”

3. These contracts are made manifest(表明) using the interface keyword, which declares a reference type that encapsulates the contract.

定義interface的格式如下▼

 

我也嘗試著自己定義了一個interface▼

 

之後老師辨別了Abstract Class, Interface 和 Mix-Ins

1. An interface offers an alternative to an abstract class for creating contracts among classes and their clients.

2. The difference is that abstract classes serve as the top of an inheritance hierarchy, where as interfaces may add their contract to numerous inheritance trees.

然後我建立了一個類去繼承這個介面▼

 

而如果是多個介面直接羅列在後面就好了,不過值得注意的是每個介面在繼承時都要被定義。

而我們也可以在介面與介面中繼承,這個比較寬泛,可以一對一也可以多對一。

C# 第四次作業

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.