美文网首页
C#:abstract 是什么意思

C#:abstract 是什么意思

作者: viva158 | 来源:发表于2017-04-05 13:17 被阅读0次

    abstract 修饰符可以用于类、方法、属性、事件和索引指示器(indexer),表示其为抽象成员

    abstract 不可以和 static 、virtual 一起使用

    声明为 abstract 成员可以不包括实现代码,但只要类中还有未实现的抽象成员(即抽象类),那么它的对象就不能被实例化,通常用于强制继承类必须实现某一成员


    usingSystem;

    usingSystem.Collections.Generic;

    usingSystem.Text;

    namespaceExample04

    {

    #region基类,抽象类

    publicabstractclassBaseClass

    {

    //抽象属性,同时具有get和set访问器表示继承类必须将该属性实现为可读写

    publicabstractString Attribute

    {

    get;

    set;

    }

    //抽象方法,传入一个字符串参数无返回值

    publicabstractvoidFunction(Stringvalue);

    //抽象事件,类型为系统预定义的代理(delegate):EventHandler

    publicabstracteventEventHandler Event;

    //抽象索引指示器,只具有get访问器表示继承类必须将该索引指示器实现为只读

    publicabstractCharthis[intIndex]

    {

    get;

    }

    }

    #endregion

    #region继承类

    publicclassDeriveClass : BaseClass

    {

    privateString attribute;

    publicoverrideString Attribute

    {

    get

    {

    returnattribute;

    }

    set

    {

    attribute =value;

    }

    }

    publicoverridevoidFunction(Stringvalue)

    {

    attribute =value;

    if(Event !=null)

    {

    Event(this,newEventArgs());

    }

    }

    publicoverrideeventEventHandler Event;

    publicoverrideCharthis[intIndex]

    {

    get

    {

    returnattribute[Index];

    }

    }

    }

    #endregion

    classProgram

    {

    staticvoidOnFunction(objectsender, EventArgs e)

    {

    for(inti = 0; i < ((DeriveClass)sender).Attribute.Length; i++)

    {

    Console.WriteLine(((DeriveClass)sender)[i]);

    }

    }

    staticvoidMain(string[] args)

    {

    DeriveClass tmpObj =newDeriveClass();

    tmpObj.Attribute ="1234567";

    Console.WriteLine(tmpObj.Attribute);

    //将静态函数OnFunction与tmpObj对象的Event事件进行关联

    tmpObj.Event +=newEventHandler(OnFunction);

    tmpObj.Function("7654321");

    Console.ReadLine();

    }

    }

    }

    结果:

    1234567

    7

    6

    5

    4

    3

    2

    1

    相关文章

      网友评论

          本文标题:C#:abstract 是什么意思

          本文链接:https://www.haomeiwen.com/subject/yiyxuttx.html