.NET抽象类与抽象方法示例
2013-03-11 09:56
141 查看
一个简单的演示示例,如下:
可以归纳出几点信息:
1、抽象类可以包含抽象方法和实例方法;抽象类可以没有抽象方法,但有抽象方法的类一定是抽象类。
2、抽象方法声明时没有实现体,类似于接口中声明的方法。
3、抽象方法必须在派生类中通过override覆写来实现,这点也类似于接口,但不同的是实现接口的方法不用override。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication4 { public abstract class Test1 { public void TestMethod1(string s1) { HttpContext.Current.Response.Write(s1); } protected abstract void TestMethod2(string s2); } public class Test2 : Test1 { protected override void TestMethod2(string s2) { HttpContext.Current.Response.Write(s2); } } public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Test2 t2 = new Test2(); t2.TestMethod1("aa"); } } }
可以归纳出几点信息:
1、抽象类可以包含抽象方法和实例方法;抽象类可以没有抽象方法,但有抽象方法的类一定是抽象类。
2、抽象方法声明时没有实现体,类似于接口中声明的方法。
3、抽象方法必须在派生类中通过override覆写来实现,这点也类似于接口,但不同的是实现接口的方法不用override。
相关文章推荐
- C# 写的一个泛型类和泛型方法示例
- 转载:关于Home的示例程序
- c#枚举转化示例大全,数字或字符串转枚举
- [C/C++] C语言文件读写示例程序 xcb
- 屏幕截图个人封装API简单代码示例
- 去除HTML标签删除HTML示例代码
- PLSQL collection 示例 之 varray
- PopupWindow(悬浮框)示例
- Dubbo项目开发及部署的简单示例
- Angular-Ui-Router+ocLazyLoad动态加载脚本示例
- AngularJS的过滤器示例
- 示例学习kettle之ExcelWriter步骤
- ORACLE WITH语句的用法示例
- .netRemoting 简单示例
- C#做位运算的简单示例
- 注解的一个示例
- OpenCV4Android开发之旅(二)----Eclipse调用OpenCV2.4.4的Java接口配置及示例
- Android图片旋转,缩放,位移,倾斜,对称完整示例(二)——Bitmap.createBitmap()和Matrix
- 第3章1节《MonkeyRunner源码剖析》脚本编写示例: MonkeyRunner API使用示例(原创)
- ES6学习——Promises:应用示例