C#复习⑤

2016年6月19日

22:39

Main Inheritance 继承

1.继承的语法结构

class A { // base class

int a;

public A() {...}

public void F() {...}

}

class B : A { // subclass (inherits from A, extends A)

int b;

public B() {...}

public void G() {...}

}

C#中类的继承只能是单继承,在Java中也只支持单继承,C++中支持多继承。但是C#、Java、C++均可以实现多个接口。

Single inheritance: a class can only inherit from one base class, but it can implement multiple interfaces.

某个类只能继承一个父类不能继承自结构体。

A class can only inherit from a class, not from a struct.

结构体不能被继承,但是可以实现多个接口。

Structs cannot inherit from another type, but they can implement multiple interfaces.

C#中所有的类的基类为Object类

A class without explicit base class inherits from Object.

2.Assignments and Type Checks分配和类型检查

class A {...}

class B : A {...}

class C: B {...}

  3.Overriding Methods重写方法

  只有在父类中声明为Virtual的方法才可以在子类中重写

  Only methods that are declared as virtual can be overridden in subclasses

  

  方法签名必须相同;

  Method signatures must be identical

  same number and types of parameters (including function type!)

  same visibility (public, protected, ...).

  属性和索引器同样可以被重写(对应关键字virtual 和 override);

  Properties and indexers can also be overridden (virtual, override).

  静态方法不可以被重写。

  Static methods cannot be overridden.

4.Dynamic Binding 动态绑定

动态绑定的好处:可以使用下面的方法针对不同类构造出的实例对象均有效。

class A {

public virtual void WhoAreYou() { Console.WriteLine("I am an A"); }

}

class B : A {

public override void WhoAreYou() { Console.WriteLine("I am a B"); }

}

调用举例:

A a = new B();

a.WhoAreYou();                // "I am a B"

动态绑定举例:

void Use (A x) {

x.WhoAreYou();

}

Use(new A());        // "I am an A"

Use(new B());        // "I am a B"

5.Hiding覆盖

在子类中成员函数可以被new关键字修饰;

使用new关键字修饰可以将那些和父类有相同函数名和签名的成员函数隐藏;

举例说明:

class A {

public int x;

public void F() {...}

public virtual void G() {...}

}

class B : A {

public new int x;

public new void F() {...}

public new void G() {...}

}

B b = new B();

b.x = ...;                // accesses B.x调用 b的x

b.F(); ... b.G();        // calls B.F and B.G调用的F函数和G函数

((A)b).x = ...;        // accesses A.x 调用A的x

((A)b).F(); ... ((A)b).G();         // calls A.F and A.G(although the dynamic type of (A)b is B)

//调用A的F函数和G函数,尽管(A)b的类型是B

6.Dynamic Binding (with Hiding)动态绑定(带覆盖即new关键字)

举例说明:

第一个简单的例子:

稍复杂点的例子:

7.子类中的构造函数

8.Visibility protected and internal可见性保护和internal

Protected:

在当前类中可见以及子类中可见

Visible in the declaring class and its subclasses(more restrictive than in Java)

Internal:

在当前Assembly可见

Visible in the declaring assembly (see later)

protected internal:

在当前类中、子类中、当前Assembly中可见

Visible in declaring class, its subclasses and the declaring assembly

9.抽象类和抽象方法

abstract class Stream {

public abstract void Write(char ch);

public void WriteString(string s) { foreach (char ch in s) Write(ch); }

}

class File : Stream {

public override void Write(char ch) {... write ch to disk ...}

}

注释:

抽象方法不能有实现;

Abstract methods do not have an implementation.

抽象方法隐藏着virtual关键字;

Abstract methods are implicitly virtual.

如果一个类中有抽象方法那么这个类也要声明为抽象类;

If a class has abstract methods (declared or inherited) it must be abstract itself.

抽象类不能实例化对象

One cannot create objects of an abstract class..

10.Abstract Properties and Indexers抽象属性和抽象索引器

abstract class Sequence {

public abstract void Add(object x);         // method

public abstract string Name { get; }         // property

public abstract object this [int i] { get; set; } // indexer

}

class List : Sequence {

public override void Add(object x) {...}

public override string Name { get {...} }

public override object this [int i] { get {...} set {...} }

}

重写的索引器和属性必须有和基类相同的get和set方法

Overriding indexers and properties must have the same get and set methods as in the base class

11.Sealed Classes 密封类

sealed class Account : Asset {

long balance;

public void Deposit (long x) { ... }

public void Withdraw (long x) { ... }

...

}

注释:

密封类不能扩展即继承(在Java中对应关键字final),但是可以继承自其他类;

sealed classes cannot be extended (same as final classes in Java),
but they can inherit from other classes.

重写方法可以被声明为单独的密封

override methods can be declared as sealed individually

12.Class System.Object Object类

class Object {

protected object        MemberwiseClone() {...}

public Type         GetType() {...}

public virtual bool         Equals (object o) {...}

public virtual string        ToString() {...}

public virtual int         GetHashCode() {...}

}

//Directly usable:

Type t = x.GetType();//returns a type descriptor (for reflection)

object copy = x.MemberwiseClone();//浅拷贝does a shallow copy (this method is protected)

//Overridable in subclasses:

x.Equals(y) //should compare the values of x and y

x.ToString() //should return a string representation of x

int code = x.GetHashCode(); //should return a hash code for x

Example

13.重载==和!=运算符

C#复习⑤的更多相关文章

  1. iOS总结_UI层自我复习总结

    UI层复习笔记 在main文件中,UIApplicationMain函数一共做了三件事 根据第三个参数创建了一个应用程序对象 默认写nil,即创建的是UIApplication类型的对象,此对象看成是 ...

  2. vuex复习方案

    这次复习vuex,发现官方vuex2.0的文档写得太简略了,有些看不懂了.然后看了看1.0的文档,感觉很不错.那以后需要复习的话,还是先看1.0的文档吧.

  3. 我的操作系统复习——I/O控制和系统调用

    上篇博客介绍了存储器管理的相关知识——我的操作系统复习——存储器管理,本篇讲设备管理中的I/O控制方式和操作系统中的系统调用. 一.I/O控制方式 I/O就是输入输出,I/O设备指的是输入输出设备和存 ...

  4. 复习(1)【Maven】

    终于开始复习旧知识了,有输入必然要有输出.输入和输出之间的内化过程尤为重要,在复习的同时,真正把学到的东西积淀下来,加深理解. Maven项目概念与配置 Maven是一个项目管理和综合工具.Maven ...

  5. 《CSS权威指南》基础复习+查漏补缺

    前几天被朋友问到几个CSS问题,讲道理么,接触CSS是从大一开始的,也算有3年半了,总是觉得自己对css算是熟悉的了.然而还是被几个问题弄的"一脸懵逼"... 然后又是刚入职新公司 ...

  6. JS复习--更新结束

    js复习-01---03 一 JS简介 1,文档对象模型 2,浏览器对象模型 二 在HTML中使用JS 1,在html中使用<script></script>标签 2,引入外部 ...

  7. jQuery 复习

    jQuery 复习 基础知识 1, window.onload $(function(){});   $(document).ready(function(){}); 只执行函数体重的最后一个方法,事 ...

  8. jQuery5~7章笔记 和 1~3章的复习笔记

    JQery-05 对表单和表格的操作及其的应用 JQery-06 jQuery和ajax的应用 JQery-07 jQuery插件的使用和写法 JQery-01-03 复习 之前手写的笔记.实在懒得再 ...

  9. HTML和CSS的复习总结

    HTML(Hypertext Markup Language)超文本标记语言:其核心就是各种标记!<html> HTML页面中的所有内容,都在该标签之内:它主要含<head>和 ...

  10. 2017年1月1日 java学习第二天复习

    今天是新年的第一天,以前学习没有总结习惯,学习效率和成果都很不好.  学习的过程就是反复的复习和不断学习的过程,开始今天的学习总结   学习java的第二天. 今天学习了java最基础的一些内容,照着 ...

随机推荐

  1. LeetCode - Minimum Depth of Binary Tree

    题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...

  2. MySQL 快速导入大量数据 资料收集

    一.LOAD DATA INFILE http://dev.mysql.com/doc/refman/5.5/en/load-data.html 二. 当数据量较大时,如上百万甚至上千万记录时,向My ...

  3. When using SqlDependency without providing an options value, SqlDependency.Start() must be called prior to execution of a command added to the SqlDependency instance.

    在调试SignalR程序时,提示一个异常:When using SqlDependency without providing an options value, SqlDependency.Star ...

  4. Redis系列二之事务及消息通知

    一.事务 Redis中的事务是一组命令的集合.一个事务中的命令要么都执行,要么都不执行. 1.事务简介 事务的原理是先将一个事务的命令发送给Redis,然后再让Redis依次执行这些命令.下面看一个示 ...

  5. LeetCode5:Longest Palindromic Substring

    题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...

  6. eclipse新建maven项目(2)

    本篇博文是继续之前的博文eclipse新建maven项目(1),那篇博文不在随笔在文章中.首先按照之前那篇博文进行创建maven项目操作,一系列操作下来之后发现刷新项目后会报错: 别急哈,可以解决. ...

  7. 2016 大连网赛---Function(单调栈)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5875 Problem Description The shorter, the simpl ...

  8. Scalaz(11)- Monad:你存在的意义

    前面提到了scalaz是个函数式编程(FP)工具库.它提供了许多新的数据类型.拓展的标准类型及完整的一套typeclass来支持scala语言的函数式编程模式.我们知道:对于任何类型,我们只需要实现这 ...

  9. [小北De编程手记] : Lesson 03 玩转 xUnit.Net 之 Fixture(上)

    在使用xUnit.Net Framework构建单元测试或自动化测试项目的时候,无论是针对一些比较耗费资源的对象亦或是为了支持Test case预设数据的能力,我们都需要有一些初始化或是清理相关的动作 ...

  10. ssh架构简单解释和vo po解释

      Struts.spring.Hibernate在各层的作用 1)struts 负责 web层. ActionFormBean 接收网页中表单提交的数据,然后通过Action 进行处理,再Forwa ...