C# 学习笔记(Class) - 03

来源:互联网 发布:网络黑侠 自己玩死 编辑:程序博客网 时间:2024/06/05 10:42

Singly Rooted Object Hierarchy

Single based class approach is endorsed by object-oriented programming(OOP) theorists and is implemented in most mainstream object-oriented languages.

A single rooted object hierarchy is the key to the unified type system, because it guarantees that each object in the hiearach have a common interface and that everything in the hierarchy will ultimately have the same base type.

Each and every object in heirarachy - and, crucially, in the hierarchies of third-party .NET code - has a certain mininual set of functionality.

Type Safety

  • Every reference to an object is typed and the object it's referncing is also typed
  • Because the CTS tracks every type in the system, there is no way to trick the system into thinking one type is actually another.
  • Each type is responsible for defining the accessibilty of its member by specifying an access modifier.

Defining Class

[attribute] [modifiers] class <class name> [: base class name]
{
       [class body]
}[;]

Fields: A field is a member varible used to hold value.
Modifiers: static, readonly, const
Method: Method is actually code that act's on the object's data( or field values)
Properties: Propertis are sometimes called smart field because they're actually methods that looks like fields to the class's clients.
Constants: a constant is a field with a value that can not be changed.
Indexers: Just as a property is a smart field, an indexer is sometimes referred to as a smart array - that is, a memeber that enable you to work with a class that is logically an array of data as thought the class itself were an array.
Event: An event cause some piece of code to run.
Operators: C sharp give you the ability via operator overloading, to add the standard mathematical operators to a class so that you can write more intuitive code.

Access Modifiers:  Public Protect Private(default) Internal

The Main Method:

servers as an entry point of a application; must be defined static; can be place in any class.

problam domain + entry point class

Command-Line Arguments:

string array type at Main method only argument

To process that parameters as flags and switches, you need program the ability yourself

ldarg.   Pushes argument at specified index
ldlen.   Pushes the length of an array
conv.i  Convert value to native int, pushing resulting native int
local variables begin with CS$ are used for the foreach statement.

The return statement terminates the execution of the method, and the returned value is used as an error level to the calling application or batch file to indicate user-defined success or failure.

C# include a mechanism by which you can define more than one class with a Main method, you can use /main:<classname> switch with the C# complier to specify which class's Main method to use.
Attempting to compile an application consisting of multiple classes with defined Main method and not specified the .main switch will result in a complier error.

Constructor

The benefit of using a constructor is that it guarantees that object will go through proper initialization before being used.

  • must have the same name as the class itself
  • don't return value and can't be prefixed with a type

<class> <object> = new <class><constructor arguments>

object can be created in c# only by the new keyword.

static member:

  • only one copy of static member exists
  • static members that are value types must have a valid type value. If you don't initialize the variable, the common language runtime will do so upon application start up by using the default value 0.

Constructor Initializers

All c# constructors -- with the exception of System.Object constructors -- include invocation of the base's constructor immediately before the execution of the first line of the constructor.

base(...)   call the class's base class constructor
this(...)    call another constructor defined with in itself

Overload methods are two or more methods with the same name but different argument lists.

The rule ensures that classes that are based on(drived from) other classes get properly initialized before their code is allowed to execute.

Specifying run-time information in a constructor initializer
Simply conde a static method and use it along with the base keyword

Constants vs Read-Only Fields

Constants -- represented by the keyword const -- are the fields that remain constant for the life of application.

Three rules

  • A constant is a member whose value is set at compile time
  • A constant value must be written as a literal
  • To define a field as a constant, simply specify the const keyword before the field being defined.

However, that works only if you know the value at compile time

Read-Only: A field with a value that won't be know until run-time and should not be changed once it's been initialized.

When you deifne a field with the readonly keyword, you have the ability to set the field's value in one place: The Constructor

Read-only fields are very similar to constants except that they can be initialized in theris encasing class's constructor

const.   static literal
readonly.  init only

Read-only field is not static.

"Constant" which is static by definition can be initialized at run time

In that case you can define the field with both readonly and static keyword.

Static Constructor is used to initialize static fields, read-only or otherwise and is always public by definition.

Type Initializer

The rules of this methos are that it be static, take no pararmeters, return no value, be decrorated with rtspecialname and specialname attribute and be named .cctor. In this case, any time a field is marked as both initonly and static, it can be modified only inside a type initializer.

Inheritance

use the following syntax:

class <derived class> : <base class>

members defined by public, protected, internal can be inherited

constructor can't be inherited, each calss must implement its own constructor irrespective of its base class.

Multiple Inheritance

  • C# doesn't support mutiple inheritance through derivation
  • The point is the only way you can implement something like multiple inheritance in C# is through the use of interface.

Sealed class

Sealed: a class can't be used as base class

an abstract class can't be used as a sealed class.

It's possible to transform virtual function memeber invocations on sealed class instance into nonvirtual invocation because the compiler guarantees the class can not have derived class.