수업 016 (04.27 2020) 메서드 재정의 (virtual / override)
2020. 4. 28. 02:39
728x90
virtual
- Operator preceding the parent class
- Can be redefined by child class
override
- Operator preceding the child class.
- is required to extend or modify the abstract of virtual implementation of an inherited method, property, indexer, or event.
- provides a new implementation of a member is inherited from a base class.
- Often(Always?) dependent on parent class's function => can call the parent class.
- must match the parent class of function, name, and signature.
- Tony Stark creates an ArmorSuite class with a method called Initialize ().
- And he decided to upgrade this weapon class.
- Create a derived class that inherits ArmorSuite. (:)
- IronMan and WarMachine new weapon with new function.
- They cannot equip their weapons with the Initialize () method inherited from ArmorSuite.
- So they need to Overriding! for equip their weapons
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
|
using System;
namespace example
class ArmorSuite
// 'ArmorSuit' Basic Fuction : Equipped with gloves to protect people
{
public virtual void Initialize()
{
}
}
class IronMan : ArmorSuite // new function with diff weapon
{
public override void Initialize()
{
base.Initialize(); // also need armed inherited from ArmorSuite.
Console.WriteLine("Rays Armed");
}
}
class WarMachine : ArmorSuite // new function with diff weapon
{
public override void Initialize()
{
base.Initialize();
Console.WriteLine("Cannons Armed");
}
}
|
cs |
728x90
'C# > Study' 카테고리의 다른 글
수업 017 (04.28 2020) Excel File (0) | 2020.04.28 |
---|---|
수업 016 (04.27 2020) 싱글턴 Singleton Pattern (0) | 2020.04.28 |
수업 016 (04.27 2020) 상속 (:) (0) | 2020.04.28 |
수업 016 (04.27 2020) Maple Story (0) | 2020.04.28 |
수업 015 (04.24 2020) 조건연산자(?:) F9,F5,F11 swap ref (0) | 2020.04.24 |