星空网 > 软件开发 > ASP.net

C#抽象类

  • 抽象类使用abstract修饰符声明;
  • 不能创建抽象类的实例;
  • 抽象类只能用作其他类的基类;
  • 抽象类中可以包含抽象成员和普通的非抽象成员;
  • 抽象类自己可以派生自另外一个抽象类;
  • 任何派生自抽象类的【类】,必须使用override关键字,实现该类所有的成员,除非派生类自己也是抽象类。
 
示例:
1.基类
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace 抽象类
{
   public abstract class BaseClass
    {
       public void BaseMethod()
       {
           Console.WriteLine("我是抽象类中的非抽象方法");
 
       }
       public abstract void DrivedMethod();
    }
}
 

 
2.子类
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace 抽象类
{
   public class DrivedClass:BaseClass
    {
       public override void DrivedMethod()
       {
           Console.WriteLine("我是子类【派生类】中的重写方法");
           //throw new NotImplementedException();
       }
    }
}
 
3.程序调用:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace 抽象类
{
    class Program
    {
        static void Main(string[] args)
        {
            //BaseClass baseClass = new DrivedClass();
            //baseClass.BaseMethod();
            //baseClass.DrivedMethod();
            //Console.WriteLine();
            //Console.ReadKey();
 
 
 
            DrivedClass driveClass = new DrivedClass();
            driveClass.BaseMethod();
            driveClass.DrivedMethod();
            Console.WriteLine();
            Console.ReadKey();
        }
    }
}
 

上面注释的代码,和没有注释的代码,执行的效果是一样的。
C#抽象类
 C#抽象类

 


 




原标题:C#抽象类

关键词:C#

C#
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流