• 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();
            }
        }
    }
     
    上面注释的代码,和没有注释的代码,执行的效果是一样的。
     
     
  • 相关阅读:
    闲聊js中的apply、call和arguments
    字符串操作,文件操作,英文词频统计预处理
    了解大数据的特点、来源与数据呈现方式
    带你精读你不知道的Javasript(上)(一)
    益智小游戏看你能否通关?
    网站性能优化——网页的生成过程
    带着封装的思想顺便实现楼层点亮
    CSS3 傻傻分不清楚的transition, transform 和 animation
    图片轮播的思路
    如何处理跨平台的自适应三
  • 原文地址:https://www.cnblogs.com/caofangsheng/p/5593356.html
Copyright © 2020-2023  润新知