반응형

new 와 override 차이


new 로 재정이 할 경우 업 케스팅시 부모의 것이 호출되지만 override 키워드로 재정의 하게 되면

자식 클래스의 함수가 호출됩니다


yo~!


다음 예제에서는 다른 컨텍스트의 비슷한 동작을 보여 줍니다. 이 예제에서는 Car라는 기본 클래스 하나와 이 클래스에서 파생된 두 클래스 ConvertibleCar 및 Minivan을 정의합니다. 기본 클래스에는 DescribeCar 메서드가 포함되어 있습니다. 이 메서드는 자동차에 대한 기본 설명을 표시한 다음 ShowDetails를 호출하여 추가 정보를 제공합니다. 세 클래스는 각각 ShowDetails 메서드를 정의합니다. new 한정자는 ConvertibleCar 클래스의 ShowDetails를 정의하는 데 사용됩니다. override 한정자는 Minivan 클래스의 ShowDetails를 정의하는 데 사용됩니다.




TestCars1은 다음 출력을 생성합니다. 특히 예상과 다를 수 있는 car2의 결과를 확인합니다. 개체 형식은 ConvertibleCar이지만 DescribeCar는 ConvertibleCar 클래스에 정의된 ShowDetails 버전에 액세스하지 않습니다. 해당 메서드는 override 한정자가 아니라 new 한정자로 선언되기 때문입니다. 결과적으로 ConvertibleCar 개체는 Car 개체와 동일한 설명을 표시합니다. Minivan 개체인 car3의 결과와 비교합니다. 이 경우 Minivan 클래스에서 선언된 ShowDetails 메서드가 Car 클래스에서 선언된 ShowDetails 메서드를 재정의하고, 표시되는 설명은 미니밴에 대해 설명합니다.


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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace OverrideAndNew2
{
 
    // Define the base class, Car. The class defines two virtual methods,  
    // DescribeCar and ShowDetails. DescribeCar calls ShowDetails, and each derived  
    // class also defines a ShowDetails method. The example tests which version of  
    // ShowDetails is used, the base class method or the derived class method.  
    class Car
    {
        public virtual void DescribeCar()
        {
            System.Console.WriteLine("Four wheels and an engine.");
            ShowDetails();
        }
 
        public virtual void ShowDetails()
        {
            System.Console.WriteLine("Standard transportation.");
        }
    }
 
    // Define the derived classes.  
 
    // Class ConvertibleCar uses the new modifier to acknowledge that ShowDetails  
    // hides the base class method.  
    class ConvertibleCar : Car
    {
        public new void ShowDetails()
        {
            System.Console.WriteLine("A roof that opens up.");
        }
    }
 
    // Class Minivan uses the override modifier to specify that ShowDetails  
    // extends the base class method.  
    class Minivan : Car
    {
        public override void ShowDetails()
        {
            System.Console.WriteLine("Carries seven people.");
        }
    }
 
    class Program
    {
 
        public static void TestCars1()
        {
            System.Console.WriteLine("\nTestCars1");
            System.Console.WriteLine("----------");
 
            Car car1 = new Car();
            car1.DescribeCar();
            System.Console.WriteLine("----------");
 
            // Notice the output from this test case. The new modifier is  
            // used in the definition of ShowDetails in the ConvertibleCar  
            // class.    
            ConvertibleCar car2 = new ConvertibleCar();
            car2.DescribeCar();
            System.Console.WriteLine("----------");
 
            Minivan car3 = new Minivan();
            car3.DescribeCar();
            System.Console.WriteLine("----------");
        }
 
 
        public static void TestCars2()
        {
            System.Console.WriteLine("\nTestCars2");
            System.Console.WriteLine("----------");
 
            var cars = new List<Car> { new Car(), new ConvertibleCar(), new Minivan() };
 
            foreach (var car in cars)
            {
                car.DescribeCar();
                System.Console.WriteLine("----------");
            }
        }
 
        public static void TestCars3()
        {
            System.Console.WriteLine("\nTestCars3");
            System.Console.WriteLine("----------");
            ConvertibleCar car2 = new ConvertibleCar();
            Minivan car3 = new Minivan();
            car2.ShowDetails();
            car3.ShowDetails();
        } 
 
        public static void TestCars4()
        {
            System.Console.WriteLine("\nTestCars4");
            System.Console.WriteLine("----------");
            Car car2 = new ConvertibleCar();
            Car car3 = new Minivan();
            car2.ShowDetails();
            car3.ShowDetails();
        }
 
        public static void Main(string[] args)
        {
            TestCars1();
 
            TestCars2();
 
            TestCars3();
 
            TestCars4();
        }
 
    }
 
 
 
}
 

cs


결과
TestCars1
----------
Four wheels and an engine.
Standard transportation.
----------
Four wheels and an engine.
Standard transportation.
----------
Four wheels and an engine.
Carries seven people.
----------
TestCars2
----------
Four wheels and an engine.
Standard transportation.
----------
Four wheels and an engine.
Standard transportation.
----------
Four wheels and an engine.
Carries seven people.
----------
TestCars3
----------
A roof that opens up.
Carries seven people.
TestCars4
----------
Standard transportation.
Carries seven people.



ref : https://docs.microsoft.com/ko-kr/dotnet/csharp/programming-guide/classes-and-structs/knowing-when-to-use-override-and-new-keywords


반응형

+ Recent posts