类在声明同时可以包含声明继承关系
如何没有声明基类,则默认为从object继承
明确声明类时,需要描述类的各种约束
不可以直接派生的类
System.Array
System.Delegate
System.Enum
System.ValueType
派生类和父类可以有同样级别的访问修饰
类的默认声明是内部的,但可以显式声明
1 // 显示声明类为内部类
2 internal class WarehouseBase//仓库
3 {
4
5 }
6
7 //默认方式声明的类也是内部类
8 abstract class WarehouseBase//仓库
9 {
10
11
12 }
2 internal class WarehouseBase//仓库
3 {
4
5 }
6
7 //默认方式声明的类也是内部类
8 abstract class WarehouseBase//仓库
9 {
10
11
12 }
子类(即派生类)的访问修饰不能比基类(即父类)高
1 public abstract class WarehouseBase//仓库
2 {
3
4
5 }
6
7 //public->internal可以
8 internal class AutomaticWarehouse : WarehouseBase //自动化仓库
9 {
10
11 }
12
13
14 //internal->public不可以
15 //可访问性不一致: 基类“Space.AutomaticWarehouse”比类“Space.SolidWarehouse”的可访问性低
16 public class SolidWarehouse : AutomaticWarehouse//立体仓库
17 {
18
19 }
20
21
22 //public->public可以
23 public sealed class VirtualWarehouse : WarehouseBase //虚拟仓库
24 {
25
26 }
2 {
3
4
5 }
6
7 //public->internal可以
8 internal class AutomaticWarehouse : WarehouseBase //自动化仓库
9 {
10
11 }
12
13
14 //internal->public不可以
15 //可访问性不一致: 基类“Space.AutomaticWarehouse”比类“Space.SolidWarehouse”的可访问性低
16 public class SolidWarehouse : AutomaticWarehouse//立体仓库
17 {
18
19 }
20
21
22 //public->public可以
23 public sealed class VirtualWarehouse : WarehouseBase //虚拟仓库
24 {
25
26 }
类不可以显式声明为 private、protected 或 protected internal
1 //错误
2 private abstract class WarehouseBase//仓库
3 {
4
5
6 }
7
8 //错误
9 protected class AutomaticWarehouse : WarehouseBase //自动化仓库
10 {
11
12 }
13
14
15 //错误
16 protected internal class SolidWarehouse : AutomaticWarehouse//立体仓库
17 {
18
19 }
2 private abstract class WarehouseBase//仓库
3 {
4
5
6 }
7
8 //错误
9 protected class AutomaticWarehouse : WarehouseBase //自动化仓库
10 {
11
12 }
13
14
15 //错误
16 protected internal class SolidWarehouse : AutomaticWarehouse//立体仓库
17 {
18
19 }
但可以在类的内部显示的声明private、protected 或 protected internal
1 public class AutomaticWarehouse : WarehouseBase //自动化仓库
2 {
3 protected internal class SolidWarehouse : AutomaticWarehouse//立体仓库
4 {
5
6 }
7
8 private sealed class PlaneWarehouse : WarehouseBase //平面仓库
9 {
10
11 }
12
13 protected internal sealed class Stack : WarehouseBase
14 {
15
16 }
17 }
2 {
3 protected internal class SolidWarehouse : AutomaticWarehouse//立体仓库
4 {
5
6 }
7
8 private sealed class PlaneWarehouse : WarehouseBase //平面仓库
9 {
10
11 }
12
13 protected internal sealed class Stack : WarehouseBase
14 {
15
16 }
17 }
本文转自shyleoking 51CTO博客,原文链接:http://blog.51cto.com/shyleoking/806298