在ASP.net中学习C#时,我很难理解几个类.在哪种情况下,我应该使用以下类private,public,protected,abstract,static,sealed?
如果有人可以用简单的例子来解释这个问题会更好.
解决方法
那些不是课.
private,protected和public都是访问修饰符.他们指出哪些其他代码可以看到他们影响的代码:
public class Foo { private int _myOwn = 1; protected int _mineAndChildren = 2; public int _everyOnes = 3; } public class Bar : Foo { public void Method() { _myOwn = 2; // Illegal - can't access private member _mineAndChildren = 3; // Works _everyOnes = 4; // Works } } public class Unrelated { public void Method() { Foo instance = new Foo(); instance._myOwn = 2; // Illegal - can't access private member instance._mineAndChildren = 3; // Illegal instance._everyOnes = 4; // Works } }
抽象类是可能包含抽象成员的类.抽象成员没有实现,所以派生类都必须实现抽象成员.
密封的类不能被继承.静态类被封装,但也只能包含静态成员.
我建议你从“Getting Started with Visual C#开始”.这是一个非常基本的问题.