美文网首页
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 是什么意思

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

  • C#中abstract的用法详解

    C#中abstract的用法详解 abstract可以用来修饰类,方法,属性,索引器和时间,这里不包括字段. 使用...

  • C# abstract virtual

    参考文档:abstractvirtual 概念:用 abstract 修饰符来指示某个类仅用作其他类的基类,而不用...

  • C#实现设计模式 —— 抽象工厂模式

    本文为转载,原文C#实现设计模式 —— 抽象工厂模式 介绍 定义 抽象工厂模式(Abstract Factory ...

  • C# abstract virtual sealed

    参考简单易懂的解释c#的abstract和virtual的用法和区别[https://blog.csdn.net/...

  • C#:extern 是什么意思

    extern 修饰符用于声明由程序集外部实现的成员函数 经常用于系统API函数的调用(通过 DllImport )...

  • 理解Abstract class和Interface

    理解Abstract class和Interface 抽象类是什么 Java中的Abstract class和In...

  • C#之abstract(抽象类)

    定义 abstract关键字,表明某个类只能是其他类的基类。可以在父类中定义一个函数,但是不去实现。所有继承自该类...

  • 动画篇----CAAnimation

    一、CAAnimation是什么 1、官方解释: CAAnimation is an abstract anima...

  • C#中abstract和interface的异同

    ● 抽象类abstractabstract可以用来修饰类,方法,属性,索引器和时间,这里不包括字段. 使用ab...

网友评论

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

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