다형성은 C#의 중요한 개념 중 하나입니다. 다형성에는 컴파일 타임과 런타임이라는 두 가지 유형이 있습니다. 이를 달성하기 위해 각각 오버로딩 및 재정의 개념이 사용됩니다. 재정의에서 하위 클래스는 다른 방식으로 상위 클래스 메서드를 구현할 수 있지만 하위 클래스 메서드는 상위와 동일한 이름과 동일한 메서드 서명을 갖는 반면 오버로드에서는 동일한 이름과 다른 매개 변수를 가진 클래스에 여러 메서드가 있습니다. 🎜>
C#에서 재정의 및 오버로드는 어떻게 작동하나요?재정의
구문:
class Parent { public virtual void Example() // base class { Console.WriteLine("parent class"); } } class Child: Parent { public override void Example() // derived class { base.Example(); Console.WriteLine("Child class"); } }
예시 #1
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OverridingExample { class Subject // base class { public virtual void study() // base class method { Console.WriteLine("Study all the subjects"); } } class Mathematics: Subject // derived class { public override void study() // derived class method { Console.WriteLine("Study Mathematics"); } } class Program { // main method static void Main(string[] args) { Subject s = new Mathematics(); s.study(); Console.ReadLine(); } } }
출력:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OverridingExample { class Subject // base class { public virtual void study() // base class method { Console.WriteLine("Study all the subjects"); } } class Mathematics: Subject // derived class { public override void study() // derived class method { base.study(); Console.WriteLine("Study Mathematics"); } } class Program { // main method static void Main(string[] args) { Mathematics m = new Mathematics(); m.study(); Console.ReadLine(); } } }
출력:
기억할 사항:
예시 #1
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OverloadingExample { class Demo { public int Sum(int x, int y) { int value = x + y; return value; } public int Sum(int x, int y, int z) { int value = x + y + z; return value; } public static void Main(string[] args) // main method { Demo d = new Demo(); int sum1 = d.Sum(24, 28); Console.WriteLine("sum of the two " + "integer value : " + sum1); int sum2 = d.Sum(10, 20, 30); Console.WriteLine("sum of the three " + "integer value : " + sum2); Console.ReadLine(); } } }
출력:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OverloadingExample { class Demo { public int Sum(int x, int y, int z) { int value = x + y + z; return value; } public double Sum(double x, double y, double z) { double value = x + y + z; return value; } public static void Main(string[] args) // main method { Demo d = new Demo(); int sum1 = d.Sum(24, 28,7); Console.WriteLine("sum of the two " + "integer value : " + sum1); double sum2 = d.Sum(10.0, 20.0, 30.0); Console.WriteLine("sum of the three " + "integer value : " + sum2); Console.ReadLine(); } } }
출력:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OverloadingExample { class Demo { public void Details(String name,int id) { Console.WriteLine("Name " + name + ", " + "Id " + id); ; } public void Details(int id,string name) { Console.WriteLine("Name " + name + ", " + "Id " + id); } public static void Main(string[] args) // main method { Demo d = new Demo(); d.Details("John", 10); d.Details("Joe", 20); Console.ReadLine(); } } }
출력:
기억할 사항:
위 내용은 C#의 오버로딩 및 재정의의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!