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();
            }
        }
    }
     
    上面注释的代码,和没有注释的代码,执行的效果是一样的。
     
     
  • 相关阅读:
    1 < 2 < 3为true, 3 > 2 > 1为false
    我的第五代选择器Icarus
    浮动不换行
    教你游泳,不会游的看了包你学会!!! 分享
    node.js 一个简单的页面输出
    天将降大任于斯人也,必先苦其心志,劳其筋骨,饿其体肤,空乏其身,行拂乱其所为,所以动心忍性,增益其所不能
    setTimeout和setInterval的使用
    JS window.open()属性
    车牌识别及验证码识别的一般思路
    苹果开发者账号注册流程
  • 原文地址:https://www.cnblogs.com/caofangsheng/p/5593356.html
Copyright © 2011-2022 走看看