抽象
抽象方法必须在派生类中重写,这一点和接口类似,虚方法不必
抽象方法只能在抽象类中声明
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
Son s = new Son();
s.Oprint();
s.Wr();
// father f = new father();不能实例化
father f = s;
f.Oprint();
f.Wr();
}
}
abstract class father
{
public virtual void Oprint()//虚方法
{
Console.WriteLine("调用了father的输出");
}
public abstract void Wr();
}
class Son : father
{
public override void Oprint()
{
Console.WriteLine("调用了Son的输出");
}
public override void Wr()
{
Console.WriteLine("Son");
}
}
}

多态就是父类定义的抽象方法,在子类对其进行实现之后,C#允许将子类赋值给父类,然后在父类中,通过调用抽象方法来实现子类的具体功能
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
father f;
f = new Son();
f.Oprint();
f.Wr();
}
}
abstract class father
{
public virtual void Oprint()//虚方法
{
Console.WriteLine("调用了father的输出");
}
public abstract void Wr();
}
class Son : father
{
public override void Oprint()
{
Console.WriteLine("调用了Son的输出");
}
public override void Wr()
{
Console.WriteLine("Son");
}
}
}

接口与抽象类
同: 都包含可以由子类继承的抽象成员 不能直接实例化 异 抽象类可以拥有抽象成员和非抽象成员;接口所有成员必须是抽象的 抽象成员可以是私有的,接口成员一般都是公有的 接口不能含有构造函数,析构函数,静态成员和常量 c#只支持单继承,即子类只能继承一个父类,而一个子类却能继承多个接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
father f;
f = new Son();
f.Oprint();
f.Wr();
}
}
interface father
{
void Oprint();
void Wr();
}
class Son : father
{
public void Oprint()
{
Console.WriteLine("调用了Son的输出");
}
public void Wr()
{
Console.WriteLine("Son");
}
}
}
来源:https://www.cnblogs.com/xcfxcf/p/12563908.html