[c#] inheritance practice1
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Parent
{
public string FirstName;
public string LastName = "Kim";
public virtual void Run()
{
Console.WriteLine("Parent({0} {1}) is Running", FirstName, LastName);
}
}
class Child : Parent
{
public override void Run()
{
Console.WriteLine("Child({0} {1}) is Running", FirstName, LastName);
}
}
class Program
{
static void Main(string[] args)
{
Parent p1 = new Parent();
p1.FirstName = "Jone";
p1.Run();
Child c1 = new Child();
c1.FirstName = "Jonas";
c1.Run();
//자식은 부모타입으로 생성해도 된다
Parent c2 = new Child();
c2.FirstName = "Rowin";
c2.Run();
Console.WriteLine("c2 Type:" + c2.GetType());
//부모를 자식타입으로 생성하면 안된다
//Child c3 = new Parent();
//c3.FirstName = "Rohas";
//c3.Run();
Console.ReadLine();
}
}
}
| cs |
댓글
댓글 쓰기