Warning: Undefined array key 0 in /www/wwwroot/w/show/success.php on line 266
Warning: Trying to access array offset on value of type null in /www/wwwroot/w/show/success.php on line 266
Warning: Undefined array key 0 in /www/wwwroot/w/show/success.php on line 266
Warning: Trying to access array offset on value of type null in /www/wwwroot/w/show/success.php on line 266
10.1.2 继承的特性
C#中的继承主要有以下3种特性。
1.继承的可传递性
如果C从B中派生,B又从A中派生,那么C不仅继承了B中声明的成员,同样也继承了A中的成员。如【范例10-1】中DoubleStudent类的基类是Student,Student的基类是Person,因此DoubleStudent类可以继承Person类中的成员。派生类是对基类的扩展,在派生类中可以添加新成员,但不能去除已经继承的成员。
2.继承的单一性
继承的单一性是指派生类只能从一个基类中继承,不能同时继承多个基类。C#不支持类的多重继承,也就是说儿子只能有一个亲生父亲,不能同时拥有多个亲生父亲。可以通过接口实现多重继承。
3.继承中的访问修饰符
在前面已讲到,C#中的访问修饰符有public、protected、private、internal和protected internal 5种,可以使用这些访问修饰符指定5个可访问性级别:public、protected、internal、protected internal和 private。在继承时各个访问修饰符的访问权限如下表所示。
| 访问性修饰符 | 类内部 | 派生类 | | :----- | :----- | :----- | :----- | :----- | | public | 访问不受限制 | 不受限制 | | protected | 访问仅限于包含类或从包含类派生的类型 | 可以访问 | | internal | 访问仅限于当前项目 | 可以访问 | | protected internal | 访问仅限于从包含类派生的当前项目或类型 | 可以访问 | | private | 访问仅限于包含类型 | 不可访问 |
基类中的成员如果用public修饰,任何类都可以访问;如果用private修饰,它将作为私有成员,只有类本身可以访问,其他任何类都无法访问。在C#中,我们使用protected修饰符,使用这个访问修饰符的成员可以被其派生类访问,而不允许其他非派生类访问。
Warning: Undefined array key 0 in /www/wwwroot/w/show/success.php on line 276
Warning: Trying to access array offset on value of type null in /www/wwwroot/w/show/success.php on line 276
Warning: Undefined array key 0 in /www/wwwroot/w/show/success.php on line 276
Warning: Trying to access array offset on value of type null in /www/wwwroot/w/show/success.php on line 276