[C#]接口Interface快速Demo
参考代码1:
using System; namespace InterfaceDemo
{
/// <summary>
/// 定义一个爬树接口
/// </summary>
interface IClimbTree
{
void ClimbTree();
} /// <summary>
/// 定义一个抓老鼠接口
/// </summary>
interface ICatchMouse
{
void CatchMouse();
}
/// <summary>
/// 定义站立行走接口
/// </summary>
interface IStandWalk
{
void StandWalk();
} /// <summary>
/// 定义一个宠物类
/// </summary>
public class Pet
{
protected string Gene = "Pet";
public void Run()
{
Console.WriteLine("我会跑");
}
}
/// <summary>
/// 猫类,继承于宠物类,又实现了爬树和抓老鼠接口
/// </summary>
public class Cat : Pet, IClimbTree, ICatchMouse
{
public void ClimbTree()
{
Console.WriteLine("我会爬树");
}
public void CatchMouse()
{
Console.WriteLine("我会抓老鼠");
}
} /// <summary>
/// 站立猫类,继承于猫类,又实现了站立行走接口
/// </summary>
public class StandCat :Cat, IStandWalk
{
public void StandWalk()
{
Console.WriteLine("我会站立行走");
}
public void WhoAreYou()
{
Console.WriteLine(Gene + " Class Animal");
}
} class Program
{
static void Main(string[] args)
{
Cat cat = new Cat();
cat.Run();
cat.ClimbTree();
cat.CatchMouse();
StandCat standCat = new StandCat();
standCat.StandWalk();
standCat.WhoAreYou();
Console.ReadLine();
}
}
}
参考代码2:
using System; namespace LearnFromComradeLeiFeng
{
public class School : IPartyCenter, IStateCouncil
{
public void NormalTeach()
{
Console.WriteLine("学校正常教学工作...");
}
void IPartyCenter.LearnFromComradeLeiFeng()
{
Console.WriteLine("学校具体落实向雷锋同志学习的号召....");
}
public void EpidemicPreventionWork()
{
Console.WriteLine("学校具体落实全校抗疫的指导方针....");
}
}
public class Army : IPartyCenter, IStateCouncil
{
public void ReadyToFight()
{
Console.WriteLine("时刻准备打仗...");
}
void IPartyCenter.LearnFromComradeLeiFeng()
{
Console.WriteLine("军队具体落实向雷锋同志学习的号召....");
}
public void EpidemicPreventionWork()
{
Console.WriteLine("军队具体落实全军抗疫的指导方针....");
}
} interface IPartyCenter
{
public void LearnFromComradeLeiFeng();
} interface IStateCouncil
{
void EpidemicPreventionWork();
}
class Program
{
static void Main(string[] args)
{
School sc = new School();
Army am = new Army();
sc.NormalTeach();
sc.EpidemicPreventionWork();
am.ReadyToFight();
am.EpidemicPreventionWork();
IPartyCenter pc = null;
pc=(IPartyCenter) sc;
((IPartyCenter)pc).LearnFromComradeLeiFeng();
pc = (IPartyCenter)am;
((IPartyCenter)pc).LearnFromComradeLeiFeng();
}
}
}
[C#]接口Interface快速Demo的更多相关文章
- MyBatis的接口式编程Demo
很久没细看过MyBatis了,时间一长就容易忘记. 下面是一个接口式编程的例子. 这里的例子一共分为4步: 1 首先要有一个namespace为接口的全类名的映射文件,该例中是 IMyUser.xml ...
- 0507 构造代码块和static案例,接口interface
0507构造代码块和static案例,接口interface [重点] 1.局部变量,成员变量,静态变量的特点 2.接口 接口语法:interface A {} 接口内的成员变量[缺省属性]publi ...
- 【Golang】Go 通过结构(struct) 实现接口(interface)
一.通过结构(struct) 实现 接口(interface) 1.在了解iris框架的时候,经常看到有这样去写的使用一个空结构体作为接收器,来调用方法,有点好奇这样做有什么意义. 解释:在 Go 语 ...
- java中的接口interface
关于接口 接口描述了实现了它的类拥有什么功能.因为Java是强类型的,所以有些操作必须用接口去约束和标记.接口作为类的能力的证明,它表明了实现了接口的类能做什么. 类似与class,interface ...
- php中的抽象类(abstract class)和接口(interface)
一. 抽象类abstract class 1 .抽象类是指在 class 前加了 abstract 关键字且存在抽象方法(在类方法 function 关键字前加了 abstract 关键字)的类. 2 ...
- 14 接口-interface的定义与实现
接口的基本语法一: 1.使用interface 定义 2.接口当中的方法都是抽象方法 3.接口当中的方法都是public权限 接口的定义: interface USB { public void re ...
- C#编程利器之三:接口(Interface)【转】
C#编程利器之三:接口(Interface) C#接口是一个让很多初学者容易迷糊的东西,用起来好象很简单,定义接口,然后在里面定义方法,通过继承与他的子类来完成具体的实现.但没有真正认识接口的作用的时 ...
- 为什么不能把委托(delegate)放在一个接口(interface)当中?
stackoverflow上有人问,为什么不能把委托放在一个接口当中? 投票最多的第一个答案第一句话说,“A Delegate is just another type, so you don't g ...
- java之接口interface
接口 1.多个无关的类可以实现同一个接口 2.一个类可以实现多个无关的接口 3.与继承关系类似,接口与实现类之间存在多态性 4.定义java类的语法格式 < modifier> class ...
- 【Java 基础篇】【第六课】接口interface
Java提供的这个interface的语法,目的就是将接口从类中剥离出来,构成独立的主体. 首先加入我们定义了这个杯子接口: interface Cup { void addWater(int w); ...
随机推荐
- jupyter notebook相关笔记
导出pdf时隐藏代码 参考:https://segmentfault.com/q/1010000043309446
- .net中微信、支付宝回调
需求:自助机调用接口生成二维码,用户扫描二维码付款后,回调方法里写入到数据库,自助机轮询查数据库判断用户是否付款. 1 using bk.Services.Log; 2 using bk.web.Co ...
- SVNKit使用相关工具类
SVNKit操作SVN仓库 导入依赖 <dependency> <groupId>org.tmatesoft.svnkit</groupId> <artifa ...
- java8 检查
// 检查放款日期 boolean allMatch = cdiscountMonthBillDetails.stream().map(CdiscountMonthBillDetail::getTra ...
- redis过期事件监听
1 修改 redis.conf配置文件: K Keyspace events, published with keyspace@ prefix事件 E Keyevent events, publish ...
- 01.BeanFactory实现
/** beanFactory 不会做的事:* 1.不会主动调用BeanFactory 后处理器* 2.不会主动添加Bean后处理器* 3.不会主动初始化单例(懒加载)* 4.不会解析beanFact ...
- KVM虚拟机迁移至VMWare ESXi
需求 由于服务器迁移,为维护方便,将统一使用vmware 平台管理虚拟机,因此需将kvm 虚拟机统一迁至vmware kvm 磁盘镜像转换 查看虚拟机 # virsh list --all Id Na ...
- Mysterious-GIF --- 攻防世界WP
题目描述: 附件: ps:我重命名了一下为 cindy,gif 解题过程 1.分析该GIF (1)查看图片属性 (2)strings命令查找字符串 (3)winhex查看 (4)因为是GIF,所以可使 ...
- pytorch学习笔记(4)--dataloader
batch_size:有多少张 shuffle=True:顺序不打乱 num_workers: 进程数 drop_last:最后不够64张是否舍去 import torchvision from to ...
- Oracle 用户创建,权限授予
https://blog.csdn.net/zhao05164313/article/details/124172838 grant create any view to crjp; 被授予权限的用户 ...