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();
            }
        }
    }
     
    上面注释的代码,和没有注释的代码,执行的效果是一样的。
     
     
  • 相关阅读:
    Oracle 创建表并设置主键自增
    Oracle 基本知识回顾
    关于JAVAweb的一些东西
    JAVA获取运行环境的信息
    关于正则表达式的一些东西
    关于jQuery的一些东西
    关于JS的一些东西
    thymeleaf 的使用
    小程序flex容器
    Vue组件化
  • 原文地址:https://www.cnblogs.com/caofangsheng/p/5593356.html
Copyright © 2011-2022 走看看