当前位置: 首页 > news >正文

显示接口成员

如下面的IDrivable和IStreerable都声明了TurnLeft()方法

public interface IDrivable

{

  void TurnLeft();

}

public interface IStreerable

{

  void TurnLeft();

}

如果声明一个类实现这两个接口,在类中必须定义两个TurnLeft()方法,如何区别这两个方法呢?答案是:必须显示声明这个方法属于哪个接口,如下例:


  
1 /*
2 Example8_7.cs illustrates an explicit interface member
3 implementation
4   */
5
6   using System;
7
8
9 // define the IDrivable interface
10 public interface IDrivable
11 {
12 void TurnLeft();
13 }
14
15
16 // define the ISteerable interface
17 public interface ISteerable
18 {
19 void TurnLeft();
20 }
21
22 // Car class implements the IMovable interface
23 public class Car : IDrivable, ISteerable
24 {
25
26 // explicitly implement the TurnLeft() method of the IDrivable interface
27 void IDrivable.TurnLeft()
28 {
29 Console.WriteLine( " IDrivable implementation of TurnLeft() " );
30 }
31
32 // implement the TurnLeft() method of the ISteerable interface
33 public void TurnLeft()
34 {
35 Console.WriteLine( " ISteerable implementation of TurnLeft() " );
36 }
37
38 }
39
40
41 class Example8_7
42 {
43
44 public static void Main()
45 {
46
47 // create a Car object
48 Car myCar = new Car();
49
50 // call myCar.TurnLeft()
51 Console.WriteLine( " Calling myCar.TurnLeft() " );
52 myCar.TurnLeft();
53
54 // cast myCar to IDrivable
55 IDrivable myDrivable = myCar as IDrivable;
56 Console.WriteLine( " Calling myDrivable.TurnLeft() " );
57 myDrivable.TurnLeft();
58
59 // cast myCar to ISteerable
60 ISteerable mySteerable = myCar as ISteerable;
61 Console.WriteLine( " Calling mySteerable.TurnLeft() " );
62 mySteerable.TurnLeft();
63
64 }
65
66 }

转载于:https://www.cnblogs.com/djcsch2001/archive/2011/05/04/2037042.html

相关文章:

  • Java单例多例理解
  • SQL Server 2005中解决死锁问题
  • PIX配置手册一(简单配置命令)
  • sql server 根据指定字段排序编号 update 与 order by 联合使用
  • MD5 报文摘要算法
  • 获取 Android 设备的唯一标识码
  • Android Studio 3.0项目迁移所遇到的坑
  • (转)ObjectiveC 深浅拷贝学习
  • grep过滤用法介绍(三)
  • Android Activity和Intent机制 学习笔记(显示网页,显示地图,打电话)
  • 07-JS中 li 排序
  • [WCF安全系列]从两种安全模式谈起
  • EIGRP路由协议中邻居的发现以及恢复
  • vim配色方案
  • Spring-基于构造函数的依赖注入
  • C++入门教程(10):for 语句
  • HTTP--网络协议分层,http历史(二)
  • Netty+SpringBoot+FastDFS+Html5实现聊天App(六)
  • opencv python Meanshift 和 Camshift
  • SQLServer插入数据
  • Web Storage相关
  • 百度贴吧爬虫node+vue baidu_tieba_crawler
  • 诡异!React stopPropagation失灵
  • 精益 React 学习指南 (Lean React)- 1.5 React 与 DOM
  • 力扣(LeetCode)357
  • 排序算法之--选择排序
  • 强力优化Rancher k8s中国区的使用体验
  • 一起参Ember.js讨论、问答社区。
  • 异常机制详解
  • 因为阿里,他们成了“杭漂”
  • 主流的CSS水平和垂直居中技术大全
  • AI又要和人类“对打”,Deepmind宣布《星战Ⅱ》即将开始 ...
  • 扩展资源服务器解决oauth2 性能瓶颈
  • ​ArcGIS Pro 如何批量删除字段
  • !! 2.对十份论文和报告中的关于OpenCV和Android NDK开发的总结
  • # 飞书APP集成平台-数字化落地
  • #多叉树深度遍历_结合深度学习的视频编码方法--帧内预测
  • #我与Java虚拟机的故事#连载01:人在JVM,身不由己
  • (14)目标检测_SSD训练代码基于pytorch搭建代码
  • (31)对象的克隆
  • (附源码)spring boot基于Java的电影院售票与管理系统毕业设计 011449
  • (附源码)spring boot校园拼车微信小程序 毕业设计 091617
  • (附源码)计算机毕业设计ssm基于Internet快递柜管理系统
  • (六)vue-router+UI组件库
  • (篇九)MySQL常用内置函数
  • (七)微服务分布式云架构spring cloud - common-service 项目构建过程
  • .aanva
  • .NET/C# 避免调试器不小心提前计算本应延迟计算的值
  • .py文件应该怎样打开?
  • ;号自动换行
  • ??myeclipse+tomcat
  • [ C++ ] STL_vector -- 迭代器失效问题
  • [ element-ui:table ] 设置table中某些行数据禁止被选中,通过selectable 定义方法解决
  • [ HTML + CSS + Javascript ] 复盘尝试制作 2048 小游戏时遇到的问题
  • [1]-基于图搜索的路径规划基础