zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }
     
    上面注释的代码,和没有注释的代码,执行的效果是一样的。
     
     
  • 相关阅读:
    iOS中SQLite知识点总结1
    iOS/mac开发的一些知名个人博客
    ReactiveCocoa框架学习2
    安装visual studio2017后 首次启动出现ActivityLog.xml异常解决方法
    《软工实践》第零次作业
    在Android Studio2.3中配置OpenCV4Android SDK
    【Try Kotlin】Kotlin Koans 代码笔记
    树-二叉搜索树-AVL树
    八种常见排序算法
    迷の衬衫()
  • 原文地址:https://www.cnblogs.com/caofangsheng/p/5593356.html
Copyright © 2011-2022 走看看