C#复习⑥

2016年6月19日

23:46

Main Interfaces & Delegates 接口和委托

1.接口基本语法

public interface IList : ICollection, IEnumerable {

int Add (object value);        // methods

bool Contains (object value);

...

bool IsReadOnly { get; }        // property

...

object        this [int index] { get; set; }        // indexer

}

接口相当于一个抽象类,只有签名没有实现;

Interface = purely abstract class; only signatures, no implementation.

接口可能包含方法、属性、索引器、时间(没有字段、没有常量、没有构造函数、没有析构函数、没有运算符、没有级联类型)

May contain methods, properties, indexers and events
(no fields, constants, constructors, destructors, operators, nested types).

接口成员隐藏着abstract或者virtual关键字

Interface members are implicitly public abstract (virtual).

接口成员不能为static

Interface members must not be static.

接口可以继承自其他的接口

Interfaces can inherit from other interfaces.

类和结构体可以实现多个接口

Classes and structs may implement multiple interfaces.

2.Implemented by Classes and Structs类、结构体实现接口

class MyClass : MyBaseClass, IList, ISerializable {

public int Add (object value) {...}

public bool Contains (object value) {...}

...

public bool IsReadOnly { get {...} }

...

public object this [int index] { get {...} set {...} }

}

一个类只能继承一个类但是可以实现多个接口

A class can inherit from a single base class, but can implement multiple interfaces.

一个结构体不能继承自其他的类或者结构体但是可以实现多个接口

A struct cannot inherit from any type, but can implement multiple interfaces.

接口中的每一个成员必须被实现

Every interface member (method, property, indexer) must be implemented or inherited from a base class.

实现接口内部包含的方法不必要声明为override

Implemented interface methods need not be declared as override.

实现接口内部包含的方法可以声明为abstract抽象方法

Implemented interface methods can be declared as abstract (i.e. an interface can be implemented by an abstract class).

如果子类MyClass中Add方法应该被重写那么应当声明为virtual尽管在IList中已经隐藏着virtual关键字

If Add() should be overridden in a subclasses of MyClass it must be declared as virtual (although Add() is already implicitly virtual in IList).

3.Working with Interfaces

举例:

interface ISimpleReader {

int Read();

}

interface IReader : ISimpleReader {

void Open(string name);

void Close();

}

class Terminal : ISimpleReader {

public int Read() { ... }

}

class File : IReader {

public int Read() { ... }

public void Open(string name) { ... }

public void Close() { ... }

}

ISimpleReader sr = null;        // null can be assigned to any variable of an interface type

sr = new Terminal();

sr = new File();

IReader r = new File();

sr = r;

4.Delegate委托

声明委托:delegate void Notifier (string sender);//组成:普通函数的签名加上关键字delegate

声明委托变量:Notifier greeting;

将方法分配给委托变量:

void SayHello(string sender) {

Console.WriteLine("Hello from " + sender);

}

greetings = new Notifier(SayHello);        // or just:

greetings = SayHello; // since C# 2.0

调用委托变量:greeting("John");

5.不同的方法分配

每一个方法可以分配至一个委托变量:

void SayGoodBye(string sender) {

Console.WriteLine("Good bye from " + sender);

}

greetings = SayGoodBye;

greetings("John");        // SayGoodBye("John") => "Good bye from John"

注意:委托变量可以被赋予null值;

如果委托变量为null,那么该委托变量不能被调用,否则产生异常;

委托变量其实是一种类,可以存储的数据结构中,可以传递参数

Creating a Delegate Value:

m = obj.Method; // or in long form: m = new DelegateType (obj.Method);

一个委托变量可以存储一个方法以及它的接收器,不是参数:greetings = myObj.SayHello;

如果obj是this那么可以省略:greetings = SayHello;

方法可以是静态的,但是这样需要用类名来进行实例,greetings = MyClass.StaticSayHello;

方法不能是抽象的,但是可以使virtual,override,new

方法的签名必须和委托的签名相匹配

有相同的参数个数;

有相同的参数类型,包括返回类型;

有相同的参数修饰符(value,ref/out)

6.多播委托Multicast Delegates

一个委托变量可以同时掌握着多个方法;

Notifier greetings;

greetings = SayHello;

greetings += SayGoodBye;

greetings("John");        // "Hello from John"

// "Good bye from John"

greetings -= SayHello;

greetings("John");        // "Good bye from John"

注意:

如果多播委托是一个函数,那么返回值是最后一个被调用的那个方法;

如果多播委托是out修饰的参数类型,那么参数应该是最后一个调用的返回.ref修饰的参数类型应该从所用的方法一直传递下去。

Java中实现上述功能:

7.事件

事件就是特殊的委托域;

事件与委托变量不同的地方:

只有声明事件的类才能够解除事件;

其他类只能改变事件域只能使用+= 或者 -=

class Model {

public event Notifier notifyViews;

public void Change() { ... notifyViews("Model"); }

}

class View {

public View(Model m) { m.notifyViews += Update; }

void Update(string sender) { Console.WriteLine(sender + " was changed"); }

}

class Test {

static void Main() {

Model model = new Model();

new View(model); new View(model); ...

model.Change();

}

}

事件在.NET Library中如何处理

举例:

public delegate void KeyEventHandler (object sender, KeyEventArgs e);

public class KeyEventArgs : EventArgs {

public virtual        bool        Alt { get {...} }        // true if Alt key was pressed

public virtual        bool        Shift { get {...} }        // true if Shift key was pressed

public         bool        Control { get {...} }        // true if Ctrl key was pressed

public         bool        Handled { get{...} set {...} }        // indicates if event was already handled

public         int         KeyValue { get {...} }        // the typed key code

...

}

class MyKeyEventSource {

public event KeyEventHandler KeyDown;

...

KeyDown(this, new KeyEventArgs(...));

...

}

class MyKeyListener {

public MyKeyListener(...) { keySource.KeyDown += HandleKey;}

void HandleKey (object sender, KeyEventArgs e) {...}

} 

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. 使用ajax和js无刷新改变页面内容和地址栏URL

    发现一个可以改变地址栏,而不导致页面刷新的东东. Chrome, FF测试通过,不支持IE. 实现目标 页面的跳转(前进后退,点击等)不重新请求页面 页面URL与页面展现内容一致(符合人们对传统网页的 ...

  2. 内存只有4G的MBP要怎么破

    开发工具包括浏览器都是极占内存的,没有个8G根本不行啊. 怎一个慢字了得? 补记: 放弃谷歌浏览器是正道

  3. 字符串-Alphabet

    在一些应用当中,会对字符串的字母表进行限制,在这些应用中,往往会用到如下的示例所涉及到的几点知识: public static void main(String[] args){ String[] a ...

  4. No.017:Letter Combinations of a Phone Number

    问题: Given a digit string, return all possible letter combinations that the number could represent.A ...

  5. CI框架源码阅读笔记8 控制器Controller.php

    最近时间有些紧,源码阅读系列更新有些慢.鉴于Controller中代码比较少,本次Blog先更新该文件的源码分析. 在经过路由分发之后,实际的应用Controller接管用户的所有请求,并负责与用户数 ...

  6. Windows台的FailOver群集简介

    首先,您需要有一些服务器硬件方面知识. 我们介绍Windows平台的FailOver群集,以多个站点场景为例,如下图示: 八个结点NODE,Windows的FailOver群集,依赖SAN存储同步各个 ...

  7. Git远程和分支管理

    一.远程       Git是分布式版本控制系统,最重要的优点就是远程仓库托管代码.不用自己搭建一个服务器,在github上面注册一个账户就可免费获取远程仓库.      首先需要先在github上面 ...

  8. .Net加密保护工具分析介绍

    本文主要介绍一些dotNet加密保护工具的原理以及就其脱壳进行简单探讨. remotesoft protector.maxtocode..Net Reactor.Cliprotector.themid ...

  9. 国际化支持(I18N)

    本章译者:@nixil 使用国际化支持(I18N)能够使你的应用根据用户所在地区的不同选择不同的语言.下面介绍如何在引用中使用国际化. 只允许使用UTF-8 Play只支持UTF-8一种字符编码.这是 ...

  10. jshint配置(js检查)

    JSHint的选项配置 asi 如果是真,JSHint会无视没有加分号的行尾,自动补全分号一直是Javascript很有争议的一个语法特性.默认,JSHint会要求你在每个语句后面加上分号,但是如果你 ...