Вы находитесь на странице: 1из 37

|  

By Võ văn Hɠi
Tháng 09/2007
r 

h ð ð

 ð

ððð

ðð

 ð

ðð

a


h Ñ  ð
is a ð 
ð
that declares a new class.
h class-declaration:
ðð ð 
ð  
   

class  ð    
ð  

  ;

a


h Ñ  ð
may optionally
include a sequence of class modifiers:
 
  
[ new
[ public
[ protected
[ internal
[ private
[ abstract
r   


h Ñ  ð
is
permitted to declare a member with
the same name or signature as an
inherited member.
h Vhen this occurs, the derived class
member is said to i the base class
member.
h ?t specifies that the class hides an
inherited member by the same name.
r   


h The  


ðð ð , and
 ð modifiers control the
accessibility of the class.
h repending on the context in which the
class declaration occurs, some of these
modifiers may not be permitted
r   


h   members are available to all code;


h 
ðð members are accessible only from
derived classes;
h ð  members are accessible only from
within the same assembly;
h 
ðð ð  members are accessible
only from derived classes within the same
assembly;
h  ð members are accessible only from
the class itself.
r   


h The  ðð modifier is used to indicate that


a class is incomplete and intended only to be
a base class of other classes.
h Ñn abstract class differs from a non-abstract
class is the following ways:
[ Ñn abstract class cannot be instantiated, and it
is an error to use the new operator on an abstract
class. Vhile it is possible to have variables and
values whose compile-time types are abstract,
such variables and values will necessarily either be
null or contain references to instances of non-
abstract classes derived from the abstract types.
[ Ñn abstract class is permitted (but not required)
to contain abstract methods and accessors.
[ Ñn abstract class cannot be sealed.
r   


h The  modifier is used to prevent


derivation from a class.
h Ñ sealed class cannot also be an
abstract class.
h The sealed modifier is primarily used
to prevent unintended derivation, but
it also enables certain run-time
optimizations.
h  ð


ð ðð

 ð

ði
ð


ð ð

 ðð

ð


ð
ð


ðð
ð

 ðð
ð

ðð 
ðð
ð

ð ð

p
   

class Ñ

public void F() Console.VriteLine("Ñ.F"); }


public virtual void G() Console.VriteLine("Ñ.G"); }
}
class B: Ñ

new public void F()


Console.VriteLine("B.F"); }
public override void G()
Console.VriteLine("B.G"); }
}
p
   

class Test

static void Main()


B b = new B();
Ñ a = b;
a.F(); //->Ñ.F
b.F(); //->B.F
a.G(); //->B.G
b.G(); //->B.G
}
}
'  

h The list of similarities between classes


and structs is long ± structs can
implement interfaces,
h and can have the same kinds of
members as classes.
'  

h 'tructs differ from classes in several


important ways, however: structs are
value types rather than reference
types, and inheritance is not supported
for structs. 'truct values are stored
either ³on the stack´ or ³in-line´.
 

h ?nterfaces are used to define a


contract; a class or struct that
implements the interface must adhere
to this contract.
h ?nterfaces can contain methods,
properties, indexers, and events as
members.
r   

h relegates enable scenarios that C++ and


some other languages have addressed with
function pointers.
h Unlike function pointers, delegates are
object-oriented, type-safe, and secure.
h relegates are reference types that derive
from a common base class:
'ystem.relegate.
h Ñ delegate instance encapsulates a method ±
a callable entity.
h For instance methods, a callable entity
consists of an instance and a method on the
instance.
r   

h ?f you have a delegate instance and an


appropriate set of arguments, you can
invoke the delegate with the
arguments.
h 'imilarly, for static methods, a
callable entity consists of a class and a
static method on the class.
r   

h Ñn interesting and useful property of a


delegate is that it does not know or
care about the class of the object that
it references.
h Ñny object will do; all that matters is
that the method¶s signature matches
the delegate¶s.
h This makes delegates perfectly suited
for "anonymous" invocation.
r   

delegate void 'implerelegate();


//«.
class Test
static void F()
'ystem.Console.VriteLine("Test.F");
}
static void Main()
'implerelegate d = new
'implerelegate(F);
d();
}
}



h Ñn enum type declaration defines a


type name for a related group of
symbolic constants.
h Enums are typically used when for
³multiple choice´ scenarios, in which a
runtime decision is made from a
number of options that are known at
compile-time.



enum Color Red, Blue, Green }


class 'hape

public void Fill(Color color)


switch(color)
case Color.Red: ...
break;
case Color.Blue: ...
break;
case Color.Green: ...
break;
default:
break;
}
}
}

 

h C# programs are organized using


namespaces.
h Namespaces are used both as an
³internal´ organization system for a
program, and as an ³external´
organization system ± a way of
presenting program elements that are
exposed to other programs.
  

h Ñ property is a named attribute associated with an


object or a class.
h Properties are a natural extension of fields ± both are
named members with associated types, and the
syntax for accessing fields and properties is the same.
h However, unlike fields, properties do not denote
storage locations.
h ?nstead, properties have accessors that specify the
statements to execute in order to read or write their
values.
h Properties thus provide a mechanism for associating
actions with the reading and writing of an object¶s
attributes, and they furthermore permit such
attributes to be computed.
  

h ?f properties in C# can be likened to


³smart fields´, then indexers can be
likened to ³smart arrays´.
h Vhereas properties enable field-like
access, indexers enable array-like
access.
  

public class ListBox: Control

private string[] items;


public string this[int index]
get
return items[index];
}
set
items[index] = value;
Repaint();
}
}
}
 

h Events permit a class to declare


notifications for which clients can
attach executable code in the form of
event handlers.
h Events are an important aspect of the
design of class libraries in general, and
of the system-provided class library in
particular.


h ³Versioning´ actually has two different


meanings.
[ Ñ new version of a component is ³source
compatible´ with a previous version if code
that depends on the previous version can,
when recompiled, work with the new
version.
[ ?n contrast, for a ³binary compatible´
component, a program that depended on
the old version can, without recompilation,
work with the new version.


h Most languages do not support binary


compatibility at all, and many do little
to facilitate source compatibility.
h ?n fact, some languages contain flaws
that make it impossible, in general, to
evolve a class over time without
breaking some client code.


// Ñuthor Ñ
namespace Ñ

class Base // version 1


}
}
// Ñuthor B
namespace B

class rerived: Ñ.Base

public virtual void F()


'ystem.Console.VriteLine("rerived.F");
}
}
}


h The author of Base produces a new version:


// Ñuthor Ñ
namespace Ñ

class Base // version 2

public virtual void F() // added in


version 2

'ystem.Console.VriteLine("Base.F");
}
}
}


h ?f Base adds an F and ships a new


version, then the intent of a binary
version of rerived is still clear ±
rerived¶s F is semantically unrelated,
and should not be treated as an
override.
h Vhen rerived is recompiled, the
meaning is unclear± the author of
rerived may intend its F to override
Base¶s F, or to hide it.


// Ñuthor Ñ
namespace Ñ

class Base // version 2

public virtual void F() // added in


version 2

'ystem.Console.VriteLine("Base.F");
}
}
}


// Ñuthor B
namespace B

class rerived: Ñ.Base // version 2a: new

new public virtual void F()

'ystem.Console.VriteLine("rerived.F");
}
}
}


// Ñuthor B
namespace B

class rerived: Ñ.Base // version 2b: override

public override void F()


base.F();
'ystem.Console.VriteLine("rerived.F");
}
}
}


h The author of rerived has one other option,


and that is to change the name of F, thus
completely avoiding the name collision.
h Though this change would break source and
binary compatibility for rerived, the
importance of this compatibility varies
depending on the scenario.
h ?f rerived is not exposed to other programs,
then changing the name of F is likely a good
idea, as it would improve the readability of
the program ± there would no longer be any
confusion about the meaning of F.
Ñ   

h Ñn attribute is an object that


represents data you want to associate
with an element in your program.
h The element to which you attach an
attribute is referred to as the target of
that attribute.
h Ex:
['erializable]
class My'erializableClass «}
à 




[1] C# LÑNGUÑGE REFERENCE


h By Ñnders Hejlsberg and 'cott Viltamuth
h a
  ið 

ða

ð

[2] Programming C#, 4th Edition


h By Jesse Liberty
h O'Reilly - February 2005

r
 
ð  ð
http://vovanhaiQN.googlepages.com

Вам также может понравиться