您的位置:知识库 »

abstract 是什么意思?

作者: Clark Zheng  发布时间: 2008-09-04 10:55  阅读: 10654 次  推荐: 0   原文链接   [收藏]  

系列文章导航:

静态成员和非静态成员的区别?

const 和 static readonly 区别?

extern 是什么意思?

abstract 是什么意思?

internal 修饰符起什么作用?

sealed 修饰符是干什么的?

override 和 overload 的区别?

什么是索引指示器?

new 修饰符是起什么作用?

this 关键字的含义?

可以使用抽象函数重写基类中的虚函数吗?

C#基础概念之密封类,属性访问器,接口

类和结构的区别?

C#基础概念之抽象类,接口,接口多继承

别名指示符是什么?

如何手工释放资源?

C#基础概念之P/Invoke,StringBuilder 和 String

explicit 和 implicit 的含义?

params 有什么用?

什么是反射?


 

4.abstract 是什么意思?

答: abstract 修饰符可以用于类、方法、属性、事件和索引指示器(indexer),表示其为抽象成员 abstract 不可以和 static 、virtual 一起使用声明为 abstract 成员可以不包括实现代码,但只要类中还有未实现的抽象成员(即抽象类),那么它的对象就不能被实例化,通常用于强制继承类必须实现某一成员。

示例:

Code
using System;
using System.Collections.Generic;
using System.Text;

namespace Example04
{
#region 基类,抽象类
public abstract class BaseClass
{
//抽象属性,同时具有get和set访问器表示继承类必须将该属性实现为可读写
public abstract String Attribute
{
get;
set;
}

//抽象方法,传入一个字符串参数无返回值
public abstract void Function(String value);

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

//抽象索引指示器,只具有get访问器表示继承类必须将该索引指示器实现为只读
public abstract Char this[int Index]
{
get;
}
}
#endregion

#region 继承类
public class DeriveClass : BaseClass
{
private String attribute;

public override String Attribute
{
get
{
return attribute;
}
set
{
attribute
= value;
}
}
public override void Function(String value)
{
attribute
= value;
if (Event != null)
{
Event(
this, new EventArgs());
}
}
public override event EventHandler Event;
public override Char this[int Index]
{
get
{
return attribute[Index];
}
}
}
#endregion

class Program
{
static void OnFunction(object sender, EventArgs e)
{
for (int i = 0; i < ((DeriveClass)sender).Attribute.Length; i++)
{
Console.WriteLine(((DeriveClass)sender)[i]);
}
}
static void Main(string[] args)
{
DeriveClass tmpObj
= new DeriveClass();

tmpObj.Attribute
= "1234567";
Console.WriteLine(tmpObj.Attribute);

//将静态函数OnFunction与tmpObj对象的Event事件进行关联
tmpObj.Event += new EventHandler(OnFunction);

tmpObj.Function(
"7654321");

Console.ReadLine();
}
}
}

结果:
1234567
7
6
5
4
3
2
1

0
0

热门文章

    最新文章

      最新新闻

        热门新闻