3.2 Inheritance (started)
parent
0685f1423a
commit
6635f23a7f
32
chap03.md
32
chap03.md
@ -373,6 +373,38 @@ string name = nameof(count); // name is "count"
|
||||
|
||||
## 3.2 Inheritance
|
||||
|
||||
**Subclass** inherit from **Superclass**
|
||||
|
||||
e.g. define class inheritance
|
||||
```csharp
|
||||
// Superclass
|
||||
public class Asset
|
||||
{
|
||||
public string Name;
|
||||
}
|
||||
|
||||
// Subclass
|
||||
public class Stock : Asset
|
||||
{
|
||||
public long SharesOwned;
|
||||
}
|
||||
public class House : Asset
|
||||
{
|
||||
public decimal Mortgage;
|
||||
}
|
||||
```
|
||||
|
||||
e.g. use classes
|
||||
```csharp
|
||||
Stock msft = new Stock { Name = "MSFT",
|
||||
SharesOwned = 1000 };
|
||||
Console.WriteLine(msft.Name); // MSFT
|
||||
Console.WriteLine(msft.SharesOwned); // 1000
|
||||
|
||||
House mansion = new House { Name = "Mansion",
|
||||
Mortgage = 2500000 };
|
||||
```
|
||||
|
||||
### 3.2.1 Polymorphism
|
||||
|
||||
### 3.2.2 Casting and Reference Conversions
|
||||
|
Loading…
x
Reference in New Issue
Block a user