简介
这篇文章是关于在C#中的多态性多态性是面向对象的一个重要的和基本的支柱。Programming.Polymorphism意味着许多形式。多态性是两个类型。
1。编译时的多态性/方法重载
2.Runtime多态性/方法重写。
编译时多态性
编译时多态性是类内有两种方法具有相同的名称,但应该有两个不同的参数。
运行时多态性
当我们创建了一个基类,实现运行时多态性虚拟方法和创建,overide行为的派生类国税发基类的虚方法/ 使用codenbsp;
使用代码的细节。在代码中使用的代码格式是。 Method Overloading Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Mymath
{
public int Add(int a,int b)
{
return a + b;
}
public int Add(int a, int b, int c)
{
return a + b + c;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// Default Program Class
class Program
{
static void Main(string[] args)
{
Mymath obj = new Mymath();
obj.Add();
Console.ReadLine();
}
}
Method Overiding Example
Examples From Three Classes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// Derived Class
class Cat:Pet
{
public override void faithful()
{
Console.WriteLine("I am faithful");
}
}
//Base Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Pet
{
public virtual void faithful()
{
Console.WriteLine("I am faithful");
}
}
// Default Program Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
static void Main(string[] args)
{
Cat obj = new Cat();
obj.faithful();
Console.ReadLine();
}
}