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. 画一画javascript原型链

    在javascript中,几种数据类型String,Number,Boolean,Object,Function都是函数,可称之为函数对象. 可以说拥有prototype属性的都是函数. 所有对象都拥 ...

  2. iOS——使用StroryBoard页面跳转及传值

    之前在网上搜iOS的页面跳转大多都是按回以前的那种xib的形式,但鄙人是使用storyboard的.这篇就只介绍利用storyboard进行页面跳转与传值. 新建页面 iOS的程序也是使用了MVC的思 ...

  3. C#单纯的字母数字ASCII码转换

    字母转换成数字 byte[] array = new byte[1];   //定义一组数组array            array = System.Text.Encoding.ASCII.Ge ...

  4. Redis系列三之持久化

    一.Redis持久化 Redis是一个支持持久化的内存数据库,redis需要经常将内存中的数据同步到磁盘来保证持久化. redis提供了不同级别的持久化方法: Snapshotting(快照,默认方式 ...

  5. .net5的异步

    public static class TaskAsyncHelper { /// <summary> /// 将一个方法function异步运行,在执行完毕时执行回调callback / ...

  6. 2016暑假多校联合---Windows 10

    2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on ...

  7. 把Java生成的RSA公钥、私钥转换成.NET使用的XML格式

    import java.security.KeyFactory; import java.security.interfaces.RSAPrivateCrtKey; import java.secur ...

  8. GJM : 使用浏览器的计算力,对抗密码破解 [转载]

    感谢您的阅读.喜欢的.有用的就请大哥大嫂们高抬贵手"推荐一下"吧!你的精神支持是博主强大的写作动力以及转载收藏动力.欢迎转载! 版权声明:本文原创发表于 [请点击连接前往] ,未经 ...

  9. java 多态奇怪现象,子类还没有构造完成就开始干活了,谁来帮我解释?

    java代码: package test.extend; public class Base { public Base(){ System.out.println("基类构造") ...

  10. Java资源大全

    古董级工具 这些工具伴随着Java一起出现,在各自辉煌之后还在一直使用. Apache Ant:基于XML的构建管理工具. cglib:字节码生成库. GlassFish:应用服务器,由Oracle赞 ...