While studying C# in ASP.net I have trouble understanding several classes. In which scenario should I use the following classes private,public,protected,abstract,static,sealed
?
It would be better if someone can explain these with easy examples.
Those are not classes.
private
, protected
and public
are access modifiers. They indicate which other code can see the code they affect:
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
}
}
An abstract
class is one that may contain abstract
members. An abstract
member has no implementation, so all derived classes must implement the abstract
members.
A sealed
class cannot be inherited. A static
class is sealed
, but also can only contain static
members.
I suggest you start with "Getting Started with Visual C#. This is a very basic question.
public
, private
and protected
aren't classes, they're access modifiers. They change what is allowed to access the classes that you decorate with them. They apply to classes as well as the members of classes.
public
items can be seen from anywhereprivate
classes can only be seen from within the same fileprivate
members of classes can only be seen within that classprotected
members are visible from within the class and its descendantsinternal
classes are public within the same assembly.
The abstract
keyword marks a class or method as having no implementation, and it must be overridden (with the override
keyword) in a derived type before you can use it.
A sealed
class cannot be inherited from.
Using the static
keyword on a class member indicates that the member belongs to the class itself, rather than a specific instance of it. Marking a class as static
imposes the restriction that all members of this class must also be static
.
private
, public
and protected
indicate who can access members of a class. private means no one outside the class can see it. public means everyone can see it. protected is just like private, but subclasses can access it.
class Data
{
private int counter; // only accessible to class functions
protected int id; // accessible to class and subclass functions
public string name; // accessible from all code
}
abstract
means this is not a finished class - it is meant to be used as a base for subclasses. Often there are virtual functions in its definition, functions intended to be "filled in" by a subclass.
abstract class Window
{
// cannot create objects of this class directly
// need to create sub class
}
static
on the class definition means there's only one global copy. It pretty much reverts the class to an old-style module. static
against a member indicates that it is a global member within the class, there is not a different version for every object you make of that class.
static class Configuration
{
// only one instance of the object
}
class Data
{
private static int counter; // all Data objects access this one counter
private int id; // each Data object has a different id
}
sealed
prevents subclasses being created; it can also be applied to individual functions to prevent them being overridden in a subclass.
sealed class TelephoneNumber
{
// cannot create subclass of TelephoneNumber
}
class Address
{
public sealed string FormatAddress()
{
// this function cannot be overridden on a subclass
}
}
I down't can comment your question but i have an little adition but importan information for you.
access modifiers are only an compiler-feature. every .net-programm can ignore this by using reflection and can access your private flaged classes and methodes.
An exampel:
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace ReflectPrivateMembers
{
class Program
{
static void Main(string[] args)
{
ConstructorInfo ci = typeof(Hello).GetConstructor(BindingFlags.NonPublic| BindingFlags.Instance ,null,System.Type.EmptyTypes,null);
object helloObject = ci.Invoke(System.Type.EmptyTypes);
MethodInfo[] helloObjectMethods = helloObject.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly| BindingFlags.Instance );
foreach (MethodInfo mi in helloObjectMethods)
{
mi.Invoke(helloObject, System.Type.EmptyTypes);
}
Console.ReadLine();
}
}
public class Hello
{
private Hello()
{
Console.WriteLine("Private Constructor");
}
public void HelloPub()
{
Console.WriteLine("Public Hello");
}
private void HelloPriv()
{
Console.WriteLine("Private Hello");
}
}
}
Source: Reflection with Private Members
The first three are access modifiers, they can be applied to types and members. private means something can only be accessed from within its declaring type. protected means something can be accessed by inheritors of the declaring type, or by something in the type itself. public means something can be accessed from anywhere that has a valid reference to the declaring type.
The rest can also be applied to both types and members. abstract (on a member) means the member has no implementation (the implementation must be provided by inheritors of the type) or (on a type) that the type cannot be instantiated (only inherited). static (on a member) means that the member is shared statically by all callers, or (on a type) that the type can only contain static members and therefore cannot be instantiated (i.e. it doesn't have any instance members and therefore cannot serve any instances of itself). sealed (on an inherited virtual or abstract member) means that inheritors of the type cannot override the member, or (on a type) that the type cannot be inherited from.
There are a couple of other modifiers you should be aware of: internal is another access modifier that means that something can be accessed by anything within the same assembly (or project) as the declaring type.
Also, virtual (on a member) means that the member may optionally be overridden by an inheritor of the type, but supplies its own default implementation. partial (on a member) allows you to provide the signature of a member in one file, and the implementation in another, or (on a type) allows you to split the definition of a type across multiple code files.
What you have there are modifiers, not types of classes.
private
, public
, and protected
are Access Modifiers. They define how accessible you want the code to be.
Before looking into the other modifiers you have listed, I would suggest attempting to get a grasp on Object Oriented Programming. Here is a link full of good resources you can review.
private,public,protected are the access modfier supported by c# language : here is the msdn link for more detail Access Modifiers
Abstract and Sealed Classes and Class Members
Static Classes and Static Class Members
private
and public
refer to the visibility of the class outside the assembly (e.g. DLL or EXE) it lives in. protected
is a modifier for methods, it means only classes that inherit from the declarator can call the method.
abstract
identifies a class that cannot be created directly, and is designed only to provide a base for other classes.
Applied to methods, static
means they can be accessed as part of the type, rather than an instance of the class:
class Bar {
static void Foo() { ... }
void Foo() { ... }
}
The first one can be called like this:
Bar.Foo();
The second one only like this:
Bar b = new Bar();
b.Foo();
Applied to classes, static
limits the class to contain only static methods. It's more aesthetic than anything else, but it also helps the compiler.
The sealed
modifier tells the compiler that a class cannot be inherited from.
精彩评论