C#之接口
接口类Interface1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1 { /*
接口:属性、方法、事件、所引器 默认为public 但不能包含常数、字段、
运算符、实例构造函数、析构函数或其他类型、任何种类的静态成员
不能包含字段
成员不允许添加访问修饰符 abstract(抽象类)也不行
不能包含实现成员的任何代码
实现过程必须在实现接口的类中完成
接口可实现多重继承
*/
interface Interface1 {
//属性
//1->接口成员不允许有访问修饰符
//2->接口中不能有字段,属性声明为自动属性
string Index {
get;
set;
}
//方法
//方法不能包含方法体
void Descreption();
}
}
对接口实现的类Class1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1 {
class Class1:Interface1 {
private string _index;
////实现接口(隐式)
//public void Descreption() {
// Console.WriteLine("接口描述"+this.Index);
//}
//public string Index {
// get;
// set;
//} //接口实现(显式)
void Interface1.Descreption() {
Console.WriteLine("接口描述"+ _index);
}
string Interface1.Index {
get {
return _index;
} set {
_index = value;
}
}
}
}
调用类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Runtime.InteropServices; namespace ConsoleApplication1 {
class Program {
static void Main(string[] args){ Class1 class1 = new Class1();
////隐式的调用(具体类调用)
//class1.Index = "嘿嘿";
//class1.Descreption();
//显式的调用(接口来调用)
Interface1 interface1 = new Class1();
interface1.Index = "嘿嘿";
interface1.Descreption(); Console.ReadKey();
显式隐式实现方式并存的情况下,接口调用的是显性方法,类对象调用的是隐形方法
当一个类实现的多个接口中具有相同的方法时,用显式方法来专门实现某个接口的方法时就显得非常有用
} } }
以前写的没一点印象了,下边接着忽悠
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 接口 {
class Program {
static void Main(string[] args) {
Ijiekouable jiekou = new person();
jiekou.jikou(); Console.ReadLine();
}
}
//接口,必须实现
interface Ijiekouable {
void jikou();
}
class person:Ijiekouable {
public void jikou() {
Console.WriteLine("隐式实现接口");
}
void Ijiekouable.jikou() {
Console.WriteLine("显式实现接口");//优先级高
}
}
}
C#之接口的更多相关文章
- App开发:模拟服务器数据接口 - MockApi
为了方便app开发过程中,不受服务器接口的限制,便于客户端功能的快速测试,可以在客户端实现一个模拟服务器数据接口的MockApi模块.本篇文章就尝试为使用gradle的android项目设计实现Moc ...
- 干货来袭-整套完整安全的API接口解决方案
在各种手机APP泛滥的现在,背后都有同样泛滥的API接口在支撑,其中鱼龙混杂,直接裸奔的WEB API大量存在,安全性令人堪优 在以前WEB API概念没有很普及的时候,都采用自已定义的接口和结构,对 ...
- 12306官方火车票Api接口
2017,现在已进入春运期间,真的是一票难求,深有体会.各种购票抢票软件应运而生,也有购买加速包提高抢票几率,可以理解为变相的黄牛.对于技术人员,虽然写一个抢票软件还是比较难的,但是还是简单看看123 ...
- Java基础Map接口+Collections工具类
1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...
- Java基础Map接口+Collections
1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...
- java基础_集合List与Set接口
List接口继承了Collection的方法 当然也有自己特有的方法向指定位置添加元素 add(索引,添加的元素); 移除指定索引的元素 remove(索引) 修改指定索引的元素 set ...
- 【WCF】自定义错误处理(IErrorHandler接口的用法)
当被调用的服务操作发生异常时,可以直接把异常的原始内容传回给客户端.在WCF中,服务器传回客户端的异常,通常会使用 FaultException,该异常由这么几个东东组成: 1.Action:在服务调 ...
- PHP以接口方式实现多重继承(完全模拟)--学习笔记
1.UML类图: 2.PHP代码: <?php /** * Created by PhpStorm. * User: andy * Date: 16-11-23 * Time: 下午7:57 ...
- 【微框架】Maven +SpringBoot 集成 阿里大鱼 短信接口详解与Demo
Maven+springboot+阿里大于短信验证服务 纠结点:Maven库没有sdk,需要解决 Maven打包找不到相关类,需要解决 ps:最近好久没有写点东西了,项目太紧,今天来一篇 一.本文简介 ...
- 【接口开发】浅谈 SOAP Webserver 与 Restful Webserver 区别
接口,强大,简单,交互,跨越平台 下面简单阐述这两大接口思想 一 REST: REST是一种架构风格,其核心是面向资源,REST专门针对网络应用设计和开发方式,以降低开发的复杂性,提高系统的可伸缩性. ...
随机推荐
- ArcGIS Engine开发前基础知识(4)
ArcGIS不同开发方式的比较 关于GIS应用软件的开发,通常有三种方式:C/S架构.网络GIS和移动GIS.ArcGIS平台提供了对三种开发方式的支持,对于采用从C/S架构的大多数开发者来讲,首先想 ...
- Android Weekly Notes Issue #224
Android Weekly Issue #224 September 25th, 2016 Android Weekly Issue #224 本期内容包括: Google Play的pre-lau ...
- Android中使用ImageViewSwitcher实现图片切换轮播导航效果
前面写过了使用ViewFlipper和ViewPager实现屏幕中视图切换的效果(ViewPager未实现轮播)附链接: Android中使用ViewFlipper实现屏幕切换 Android中使用V ...
- 编译protobuf的jar文件
1.准备工作 需要到github上下载相应的文件,地址https://github.com/google/protobuf/releases protobuf有很多不同语言的版本,因为我们需要的是ja ...
- 用友NC开发的ListView切换成FormEdit
在点击 “修改” 按钮或者双击ListView中某一项Item,将进入FormEdit即卡片界面,但是 FormEdit有两种状态,一种是 可编辑的状态(点修改按钮进入的状态),一种是非可编辑状态(双 ...
- 《Ansible权威指南》笔记(1)——安装,ssh密钥登陆,命令
2016-12-23 读这本<Ansible权威指南>学习ansible,根据本书内容和网上的各种文档,以及经过自己测试,写出以下笔记.另,这本书内容很好,但印刷错误比较多,作者说第二版会 ...
- 《Note --- Unreal 4 --- behavior tree》
Web: https://docs.unrealengine.com/latest/INT/Engine/AI/BehaviorTrees/index.html Test project: D:\En ...
- 【Swift】Alamofile网络请求数据更新TableView的坑
写这篇BLOG前,有些话不得不提一下,就仅当发发恼骚吧... 今天下午为了一个Alamofire取得数据而更新TableView的问题,查了一下午的百度(360也是见鬼的一样),竟然没有一个简单明了的 ...
- [LeetCode] Hamming Distance 汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [LeetCode] 4Sum II 四数之和之二
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...