標籤:
這節課老師首先介紹了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# 第四次作業