clr via c# 接口
1,常用接口及其定义
public interface IDisposable{
void Dispose();
}
public interface IEnumerable}{
IEnumerator GetEnumerator();
}
public interface IEnumerable<T>:IEnumerable{
IEnumerator GetEnumerator<T> GetEnumerator()
}
public interface ICollection<T>:IEnumerable<T>,IEnumerable {
void Add(T item);
void Clear();
Boolean Contains(T item);
void CopyTo(T[] array,int32 arrayIndex);
int32 Count{get;}
Boolean IsReadOnly {get;}}
- 接口继承:接口继承表示某类如果继承该接口,则必须实现该接口的继承接口的方法.
- 比如 ,必须实现这些方法.
public class MyClassForInterface<T> : ICollection<T> where T:class,new()
{
public int Count => throw new NotImplementedException(); public bool IsReadOnly => throw new NotImplementedException(); public void Add(T item)
{
throw new NotImplementedException();
} public void Clear()
{
throw new NotImplementedException();
} public bool Contains(T item)
{
throw new NotImplementedException();
} public void CopyTo(T[] array, int arrayIndex)
{
throw new NotImplementedException();
} public IEnumerator<T> GetEnumerator()
{
throw new NotImplementedException();
} public bool Remove(T item)
{
throw new NotImplementedException();
} IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}
2,当实现接口的时候,的一些结果
public class Base : IDisposable
{
public void Dispose()//实现接口函数,未加virutal,则默认是 virtual sealed---无法重载.
{
Console.WriteLine("Base's Dispose!");
}
}
public class Derived : Base, IDisposable
{
public new void Dispose()//new,关键字表明这是重新实现的Dispose方法.override...表明是重载的方法.
{
Console.WriteLine("Derived's Dispose!");
}
}
public static class CallBaseDerivedOfInterface
{
public static void CallBaseAndDerived()
{
Base b = new Base();//b的类型,和b的对象的类型都是Base;
Derived d = new Derived();//d的类型和d的对象的类型都是Derived
b.Dispose();//调用b的类型的Dispose
d.Dispose();//调用d的类型的Dispose
((IDisposable)b).Dispose();//调用b的对象的类型的Dispose,Base.Dispose
((IDisposable)d).Dispose();//调用d的对象的类型的Dispose,Derived.Dispose
b = new Derived();//b的类型是Base,b的对象的类型是Derived
b.Dispose();//调用Base.Dispose
((IDisposable)b).Dispose();//调用b的对象的Dispose---Derived.Dispose
}
}//结果Base's Dispose!
Derived's Dispose!
Base's Dispose!
Derived's Dispose!
Base's Dispose!//----调用Base.Dispose
Derived's Dispose!//---调用Derived.Dispose
3,隐式和显式接口方法实现
public class SimpleType : IDisposable
{
public void Dispose()
{
Console.WriteLine("Dispose");
}
}
则该类的类型的方法表里将包含:
- Object定义的所有虚实列方法
- IDisposeable定义的所有接口方法.Dispose();
- SimpleType 引入的新方法Dispose();
在本列中 和上列中,调用 b.Dispose()---则调用SimpleType 定义的Dispose()方法.
调用((IDispose)b).Dispose---调用的是接口的Dispose()方法--其指向上一个方法.
public class SimpleType : IDisposable
{
public void Dispose()
{
Console.WriteLine("Dispose");
}
void IDisposable.Dispose()//显示的接口实现
{
Console.WriteLine("IDispose.Dispose");
}
}
SimpleType st = new SimpleType();
st.Dispose();
((IDisposable)st).Dispose();//只有显示的接口类型 才能够调用.//Dispose IDispose.Dispose
clr via c# 接口的更多相关文章
- CLR via C#(17)--接口
CLR不允许继承多个基类,但是可以继承多个接口.凡是能使用具名接口类型的实例的地方,都能使用实现了接口的一个类型的实例. 接口是对一组方法签名进行了统一命名,但不提供任何实现,而具体类则必须为继承的全 ...
- [CLR via C#]13. 接口
一.类和接口继承 在Microsoft.Net Framwork中,有一个名为System.Object的类,它定义了4个公共实例方法:ToString, Equals, GetHashCode和Ge ...
- <NET CLR via c# 第4版>笔记 第13章 接口
13.1 类和接口继承 13.2 定义接口 C#用 interface 关键字定义接口.接口中可定义方法,事件,无参属性和有参属性(C#的索引器),但不能定义任何构造器方法,也不能定义任何实例字段. ...
- 重温CLR(九) 接口
对于多继承(multiple inheritance)的概念,许多程序员并不陌生,他是指一个类从两个或多个基类派生的能力.例如,假定TransmitData类的作用是发送数据,ReceiveData类 ...
- CLR via c#读书笔记九:接口
1.接口对一组方法签名进行了统一命名.接口还能定义事件.无参属性和有参属性(C#的索引器). 2.c#禁止接口定义任何一种静态成员. 3.C#编译器要求将实现接口的方法标记为public.CLR要求将 ...
- CLR via C#深解笔记六 - 泛型
面向对象编程一个好处就是“代码重用”,极大提高了开发效率.如是,可以派生出一个类,让它继承基类的所有能力,派生类只需要重写虚方法,或添加一些新的方法,就可以定制派生类的行为,使之满足开发人员的需求. ...
- [CLR via C#]12. 泛型
泛型(generic)是CLR和编程语言提供一种特殊机制,它支持另一种形式的代码重用,即"算法重用". 简单地说,开发人员先定义好一个算法,比如排序.搜索.交换等.但是定义算法的开 ...
- 由浅入深学习.NET CLR 系列:目录
经过对Android的一阵折腾,些许熟悉了一些Java的东东,又开始转战.NET.我觉得学习最好和工作不要相离太远,才会更加随笔随意,索性整理一些比较系统的.NET的基础知识学习学习.一提起学习.NE ...
- CLR总览
Contents 第1章CLR的执行模型... 4 1.1将源代码编译成托管代码模块... 4 1.2 将托管模块合并成程序集... 6 1.3加载公共语言运行时... 7 1.4执行程序集的代码.. ...
随机推荐
- Dynamics 365 CRM 配置field service mobile
配置field service mobile其实微软是有官方文档的, 但是没有坑的微软产品不是好产品. 一些细节设置文中还是没有考虑到的. 所以这里带大家配置一下field service mobil ...
- ios---图片缩放
1.设置scrollview的代理 2.实现如下方法 -(UIView )viewForZoomingInScrollView:(UIScrollView )scrollView{ return se ...
- 性能优于JDK代理,CGLib如何实现动态代理
按照代理的创建时期,代理类可以分为两种. 静态代理:由程序员创建或特定工具自动生成源代码,再对其编译.在程序运行前,代理类的.class文件就已经存在了. 动态代理:在程序运行时,运用反射机制动态创建 ...
- linux下oracle调试小知识
1.oracle 安装下的/u01/diag/rdbms/orcl/HOF/incident目录下是什么文件?答:每当一个错误发生的时候,oracle会创建一个incident,并且分配一个INCID ...
- 曹工说Spring Boot源码(14)-- AspectJ的Load-Time-Weaving的两种实现方式细细讲解,以及怎么和Spring Instrumentation集成
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
- 介绍Netty
介绍Netty 概述 Netty是由JBOSS提供的一个java开源框架,现为 Github上的独立项目.Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可靠性的网络服务 ...
- 中国天气网API接口
http://www.weather.com.cn/data/sk/101010100.html http://www.weather.com.cn/data/cityinfo/101010100.h ...
- Asp.Net Core Identity 骚断腿的究极魔改实体类
前言 默认的 Identity 实体类型在大多数时候已经基本够用,很多时候也只是稍微在 IdentityUser 类中增加一些自定义数据字段,比如头像.这次,我要向园友隆重介绍我魔改之后的 Ident ...
- 并发编程的基石——AQS类
本博客系列是学习并发编程过程中的记录总结.由于文章比较多,写的时间也比较散,所以我整理了个目录贴(传送门),方便查阅. 并发编程系列博客传送门 本文参考了[Java多线程进阶(六)-- J.U.C之l ...
- 【Debian】 Debian 安装源配置
Debian 安装源配置 所有的Linux安装完后第一件事,就是要更新安装源 安装源是什么呢,安装源又称软件源,是指把软件的安装源地址放在一个pool里面,用一条命令(比如apt-get instal ...