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 10g(测试环境:windows server 2003 sp1+Oracle 10g)
    Response.Redirect报"正在中止进程"异常的处理
    快速使网页变灰色
    .NET创建Windows服务[转]
    [收集]javascript中得到当前窗口的高和宽
    [转帖]javascript版 UrlEncode和UrlDecode函数
    js异步读取xml(支持ff和xpath)
    辞职考研后记
    BCB6 E2494 Unrecognized __declspec modifier
  • 原文地址:https://www.cnblogs.com/caofangsheng/p/5593356.html
Copyright © 2011-2022 走看看