3.2.3 Virtual Function Members
parent
1496a7060d
commit
2b23027060
28
chap03.md
28
chap03.md
@ -445,6 +445,34 @@ TODO: Omit remaining content
|
|||||||
|
|
||||||
### 3.2.3 Virtual Function Members
|
### 3.2.3 Virtual Function Members
|
||||||
|
|
||||||
|
* A *function* marked as `virtual` can be **overriden** by subclasses for more specific implementation
|
||||||
|
* Who be declared `virtual`? ANS: Methods, properties, indexers, events
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
// Define a virtual method to be overridden
|
||||||
|
public class Asset
|
||||||
|
{
|
||||||
|
public string Name;
|
||||||
|
public virtual decimal Liability => 0; // Expression-bodied properties
|
||||||
|
}
|
||||||
|
|
||||||
|
// subclass override
|
||||||
|
public class House : Asset
|
||||||
|
{
|
||||||
|
public decimal Mortgage;
|
||||||
|
public override decimal Liability => Mortgage;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialization of overridden field
|
||||||
|
House mansion = new House { Name = "McMansion", Mortgage = 250000 };
|
||||||
|
Asset a = mansion;
|
||||||
|
Console.WriteLine(a.Liability); // 250000
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Covariant return types (C# 9)
|
||||||
|
|
||||||
|
TODO: Omit
|
||||||
|
|
||||||
### 3.2.4 Abstract Classes and Abstract Members
|
### 3.2.4 Abstract Classes and Abstract Members
|
||||||
|
|
||||||
### 3.2.5 Hiding Inherited Members
|
### 3.2.5 Hiding Inherited Members
|
||||||
|
Loading…
x
Reference in New Issue
Block a user