[C# Basics] Basic knowledge one: basic object-oriented knowledge.


[C# Basics] Basic knowledge one: basic object-oriented knowledge.

Inspire yourself to have time to see more.!!


The C# foundation is divided into seven parts:

  One: object-oriented

     Two: value types, reference types, string operations

  Three: Collection file operation

  Four: regular expression

  Five: XML operations

  Six: commission, event

  Seven: reflection

 

1, Object-oriented:

  Three characteristics of object-oriented: encapsulation, inheritance, polymorphism

2, What is a class? What is an object? The difference between a class and an object?

  Class: Similar to a model, determining the characteristics (attributes) and behaviors (methods) that the object will have

  An object is a concrete realization that you can see and touch - everything is an object.

    --Classes are molds, molds for creating objects, abstract

       A class is a set of data types, user-defined data types.

     .Class composition: fields, attributes, methods, constructors.

    --Object is concrete, it is a concrete instance of the class. Object has attributes (characteristics) and methods (behavior)

    --The class contains data (indicated by fields) and behaviors (indicated by methods (functions, functions), and methods are a piece of code with a name)

3. Summary of knowledge points

 

Encapsulation inheritance polymorphism

1. Package:

  Encapsulation is the first step to achieve object-oriented programming. Encapsulation is to gather data or functions in individual units (we call them classes). The encapsulated object is usually called an abstract data type. 

 The meaning of
  encapsulation : The meaning of encapsulation is to protect or prevent code (data) from being accidentally destroyed by us. In object-oriented programming, data is regarded as a central element and is closely integrated with the functions that use it, thereby protecting it from accidental modification by other functions.

  Encapsulation provides an effective way to protect data from accidental destruction. Compared to defining data (implemented by fields) in the program as public (public), we define them (fields) as private (privat) in many ways. Private data can be indirectly controlled in two ways. The first method, we use the traditional deposit and withdrawal methods. The second method we use properties (property).

  Using attributes can not only control the legality of accessing data, but also provide flexible operation methods of "read-write", "read-only", and "write-only".

Access modifier:

 

2. Inheritance:

  继承主要实现重用代码,节省开发时间。

1、C#中的继承符合下列规则:

  1. 继承是可传递的。如果C从B中派生,B又从A中派生,那么C不仅继承了B中声明的成员,同样也继承了A中的成员。Object类作为所有类的基类。

  2. 派生类应当是对基类的扩展。派生类可以添加新的成员,但不能除去已经继承的成员的定义。

  3. 构造函数和析构函数不能被继承。除此之外的其它成员,不论对它们定义了怎样的访问方式,都能被继承。基类中成员的访问方式只能决定派生类能否访问它们。

  4. 派生类如果定义了与继承而来的成员同名的新成员,就可以覆盖已继承的成员。但这并不因为这派生类删除了这些成员,只是不能再访问这些成员。

  5. 类可以定义虚文法、虚属性以及虚索引指示器,它的派生类能够重载这些成员,从而实现类可以展示出多态性。

  2、new关键字

   如果父类中声明了一个没有friend修饰的protected或public方法,子类中也声明了同名的方法。则用new可以隐藏父类中的方法。(不建议使用)

  3、base关键字

   base 关键字用于从派生类中访问基类的成员:

  1. 调用基类上已被其他方法重写的方法。

  2. 指定创建派生类实例时应调用的基类构造函数。

 三、多态

  1、多态:同一操作作用于不同的对象,可以有不同的解释,产生不同的执行结果。在运行时,可以通过指向基类的指针,来调用实现派生类中的方法。

  编译时的多态性:

  编译时的多态性是通过重载来实现的。对于非虚的成员来说,系统在编译时,根据传递的参数、返回的类型等信息决定实现何种操作。 

  运行时的多态性:

  运行时的多态性就是指直到系统运行时,才根据实际情况决定实现何种操作。C#中,运行时的多态性通过虚成员实现。 

  编译时的多态性为我们提供了运行速度快的特点,而运行时的多态性则带来了高度灵活和抽象的特点。

   2、实现多态:

  1. 接口多态性。

  2. 继承多态性。

  3. 通过抽象类实现的多态性。

  3、override关键字:

   重写父类中的virtual修饰的方法,实现多态。

五个访问修饰符

  Private:只有类本身能存取.
  Protected:类和派生类可以存取.
  Internal:只有同一个项目中的类可以存取.
  Protected Internal:是Protected和Internal的结合. 
  Public:完全存取.

  

  1. C# 方法默认访问级别 : private  

  2. C# 类默认访问级别 : internal 

  3. 接口成员访问修饰符默认为public,且不能显示使用访问修饰符。

  4. 类(class)

    构造函数默认为public访问修饰符。

    析构函数不能显示使用访问修饰符且默认为private访问修饰符。 
      析构函数往往用来做“清理善后” 的工作(例如在建立对象时用new开辟了一片内存空间,应在退出前在析构函数中用delete释放)。

  5.  

    类的成员默认访问修饰符为private; 

  6. 枚举(enum)

    枚举类型成员默认为public访问修饰符,且不能显示使用修饰符。

  7. 结构(struct) 

    结构成员默认为private修饰符。 

    结构成员无法声明为protected成员,因为结构不支持继承。 

  8. 嵌套类型

    嵌套类型的默认访问修饰符为private。 和类,结构的成员默认访问类型一致。

字段和属性及索引器

1、属性
  所谓属性其实就是特殊的类成员,它实现了对私有类域的受控访问。在C#语言中有两种属性方法,其一是get,通过它可以返回私有域的值,其二是set,通过它就可以设置私有域的值。比如说,以下面的代码为例,创建学生姓名属性,控制对name字段的受控访问:

 View Code

2、索引器
简单说来,所谓索引器就是一类特殊的属性,通过它们你就可以像引用数组一样引用自己的类。显然,这一功能在创建集合类的场合特别有用,而在其他某些情况下,比如处理大型文件或者抽象某些有限资源等,能让类具有类似数组的行为当然也是非常有用的。比如,上例中,我们假设一个班级有若干个学生,构建索引器就可以很方便地调用:

 View Code

上面代码中,我们看到索引器的访问器带一个参数(参数为整数),其实可以构建多个参数的索引器。还以上述代码为例,我们要根据学生学号和姓名得到学生的考试总分,修改后代码如下:

 View Code

3、总结:
<1>、

属性的定义:
访问修饰符 返回类型 属性名


      get{语句集合}
      set{语句集合}

索引器的定义:

访问修饰符 返回类型 this[参数类型 参数...]

       get{语句集合}
       set{语句集合}

<2>、

索引器使得对象可按照与数组相似的方法进行索引。
this 关键字用于定义索引器。
get 访问器返回值。set 访问器分配值。
value 关键字用于定义由 set 索引器分配的值。
索引器不必根据整数值进行索引,由你决定如何定义特定的查找机制。
索引器可被重载。
<3>、属性和索引器的主要区别: 
a、类的每一个属性都必须拥有唯一的名称,而类里定义的每一个索引器都必须拥有唯一的签名(signature)或者参数列表(这样就可以实现索引器重载)。 
b、属性可以是static(静态的)而索引器则必须是实例成员。
<4>、索引器重载实例:

 View Code

 

里氏替换

 一句话总结: 子类可以替换父类的位置,而程序的功能不受影响

更多链接: http://blog.csdn.net/yisuowushinian/article/details/18976629

 

分类: C#基础系列

好文要顶 关注我 收藏该文  

一枝花算不算浪漫


Guess you like

Origin blog.51cto.com/7592962/2543854