木制百叶窗,海南省儋州市政务网,envapt.dll
在现有类(基类、父类)上建立新类(派生类、子类)的处理过程称为继承。派生类能自动获得基类的除了构造函数和析构函数以外的所有成员,可以在派生类中添加新的属性和方法扩展其功能。
继承的特性:
可传递性:c从b派生,b从a派生,那么c不仅继承b也继承a。
单一性:只能从一个基类中继承,不能同时继承多个基类继承中的访问修饰符base和this关键字基类的构造函数和析构函数不能被继承的。但可以使用关键字base来继承基类的构造函数。
c#中的base关键字代表基类,使用base关键字可以调用基类的构造函数、属性和方法。
namespace inheritanceapplication { class shape { public void setwidth(int w) { width = w; } public void setheight(int h) { height = h; } protected int width; protected int height; } // 派生类 class rectangle: shape { public int getarea() { return (width * height); } } class rectangletester { static void main(string[] args) { rectangle rect = new rectangle(); rect.setwidth(5); rect.setheight(7); // 打印对象的面积 console.writeline("总面积: {0}", rect.getarea()); console.readkey(); } } }
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
asp.net搭建博客,使用BlogEngine.NET+MySql搭建博客
网友评论