Andy's blog

If you always do what you've always done, you'll always get what you've always got.

0%

2020-11-13-物件導向4

今天來認識繼承的範例


參考資料:
ITHandyGuy Tutorial


範例

今天來介紹一個繼承的範例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

public class Car
{
//Field,我們習慣將變數前面加_ 表全域變數
private string _type;
private string _registration;
private string _year;

//constructor
public Car(string type, string registration, string year)
{
_type = type;
_registration = registration;
_year = year;
}

}

public class Toyota: Car
{
//constructor //實作上層Car的Constructor
public Toyota(string registration, string year): base("001","Reg1","1998")
{

}
//new出一個物件
Car Car = new Car();
//型別轉換
(Car)Toyota.method();
}