C# abstract修饰符浅析
来源:互联网 发布:qq软件测试报告范例 编辑:程序博客网 时间:2024/04/26 04:42
C#语言有很多值得学习的地方,这里我们主要介绍C# abstract修饰符,包括介绍通常用于强制继承类必须实现某一成员。等方面。
C# abstract修饰符是什么意思?
C# abstract修饰符可以用于类、方法、属性、事件和索引指示器(indexer),表示其为抽象成员 abstract 不可以和 static 、virtual 一起使用声明为 abstract 成员可以不包括实现代码,但只要类中还有未实现的抽象成员(即抽象类),那么它的对象就不能被实例化,通常用于强制继承类必须实现某一成员。
示例:
- 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();
- }
- }
- }
- C# abstract修饰符浅析
- abstract修饰符浅析
- C#之修饰符-----abstract
- C#中virtual,abstract,override修饰符
- abstract修饰符
- Java abstract修饰符
- java修饰符:abstract
- Java abstract修饰符
- abstract修饰符
- abstract修饰符说明
- java abstract修饰符
- abstract修饰符
- abstract修饰符
- C#中继承修饰符(virtual/abstract/sealed)
- C#中继承修饰符(virtual/abstract/sealed)
- C#中继承修饰符(virtual/abstract/sealed)
- C#修饰符stattic、virtual、abstract、override、sealed、extern
- java修饰符五(abstract 修饰符)
- Apache Httpd PHP环境搭建
- javascript除去数组中的重复元素【转】
- 一步一步学grails:2 中文
- 关于两个svn服务器子目录同步问题
- symbian压缩和解压缩GZIP文件
- C# abstract修饰符浅析
- mt.exe : general error c101008d 解决方法
- 123131
- C# sealed修饰符学习笔记
- symbian 使用RLibrary加载DLL
- How to Cross Compile the Busybox
- jquery+ajax实现无刷新提交的功能
- ASP.NET上传大于4M的文件的相关设置
- RMutex symbian 线程间变量锁定