C# - 重定义一个接口的实现
using System;
using System.Collections.Generic;
using System.Text;
namespace MyTester
{
public interface IStorable {
void Read();
void Write();
}
public class Document:IStorable
{
public Document(String s) {
Console.WriteLine("Createing Document with:{0}",s);
}
//实现接口类的Read方法并声明为虚方法,可以被子类重写
public virtual void Read()
{
Console.WriteLine("Document Read Method for IStorable");
}
//实现接口类的Write方法
public void Write()
{
Console.WriteLine("Document Write Method for IStorable");
}
}
public class Note : Document {
public Note(String s):base(s) {
Console.WriteLine("Creating note with:{0}",s);
}
//重写Document类的Read方法
public override void Read()
{
Console.WriteLine("Overriding the Read method for Note");
}
//Note重新创建的Write方法,只属于Note本身
public new void Write() {
Console.WriteLine("Implementing the Write method for Note!");
}
}
public class Tester {
static void Main() {
Document theNote = new Note("Test Note");
IStorable isNote = theNote as IStorable;
if (isNote != null) {
isNote.Read();
isNote.Write();
}
Console.WriteLine("\r\n");
theNote.Read();
theNote.Write();
Console.WriteLine("\r\n");
Note note2 = new Note("Second Test");
IStorable isNote2 = note2 as IStorable;
if(isNote != null){
isNote2.Read();
isNote2.Write();
}
Console.WriteLine("\r\n");
note2.Read();
note2.Write();
Console.ReadLine();
}
}
}
C# - 重定义一个接口的实现的更多相关文章
- Java 基础 面向对象: 接口(interface )概念 以及接口之练习3 -定义一个接口用来实现两个对象的比较并 判断instanceof是否为同类
接口(interface )概念概念笔记 及测试代码: /** 面向对象: 接口(interface ) 是与类并行的一个概念: * 1.接口可以看成一个特殊的抽象类,它是常量与抽象方法的一个集合 * ...
- 定义一个接口CanFly,描述会飞的方法public void fly();
1.使用类与接口的知识完成如下要求:(1)定义一个接口CanFly,描述会飞的方法public void fly();(2)分别定义类飞机和鸟,实现CanFly接口.(3)定义一个测试类,测试飞机和鸟 ...
- C++:如何正确的定义一个接口类
C++中如何定义接口类?首先给接口类下了定义:接口类应该是只提供方法声明,而自身不提供方法定义的抽象类.接口类自身不能实例化,接口类的方法定义/实现只能由接口类的子类来完成. 而对于C++,其接口类一 ...
- 如何定义一个接口(接口Interface只在COM组件中定义了,MFC和C++都没有接口的概念)
接口是COM中的关键词,在c++中并没有这个概念.接口是一种极度的抽象.接口用在COM组件中有自己的GUID值,因此定义接口时一定要指定它的GUID值. 实际上接口就是struct,即#define ...
- 38.利用接口做参数,写个计算器,能完成+-*/运算 (1)定义一个接口Compute含有一个方法int computer(int n,int m); (2)设计四个类分别实现此接口,完成+-*/运算 (3)设计一个类UseCompute,含有方法: public void useCom(Compute com, int one, int two) 此方法要求能够:1.用传递过来的对象调用comp
//接口Compute package jieKou; public interface Compute { int Computer(int n,int m); } //加 package jieK ...
- (1)定义一个接口CanFly,描述会飞的方法public void fly(); (2)分别定义类飞机和鸟,实现CanFly接口。 (3)定义一个测试类,测试飞机和鸟,在main方法中创建飞机对象和鸟对象, 再定义一个makeFly()方法,其中让会飞的事物飞。并在main方法中调用该方法, 让飞机和鸟起飞。
package b; public interface CanFly { public void fly(); } package b; public class FeiJi implements C ...
- (1)定义一个接口Compute含有一个方法int computer(int n,int m); (2)设计四个类分别实现此接口,完成+-*/运算 (3)设计一个类UseCompute,含有方法: public void useCom(Compute com, int one, int two) (4)设计一个测试类
package b; public interface Computer { int computer(int n,int m); } package b; public class Jia impl ...
- 利用接口做参数,写个计算器,能完成+-*/运算 (1)定义一个接口Compute含有一个方法int computer(int n,int m); (2)设计四个类分别实现此接口,完成+-*/运算 (3)设计一个类UseCompute,含有方法: public void useCom(Compute com, int one, int two) 此方法要求能够:1.用传递过来的对象调用compute
package com.homework5; public interface Compute { //声明抽象方法 int computer(int n,int m); } package com. ...
- 重学 Java 设计模式:实战代理模式「模拟mybatis-spring中定义DAO接口,使用代理类方式操作数据库原理实现场景」
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 难以跨越的瓶颈期,把你拿捏滴死死的! 编程开发学习过程中遇到的瓶颈期,往往是由于看不 ...
随机推荐
- Windows 应用程序结构
Windows 应用程序结构
- last reboot
last reboot是一个linux命令,查看上次重启时间等相关信息
- 一点用JS写控制权限的心得
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Http权威指南笔记(一) URI URL URN 关系
定义 URI:统一资源标识符(Uniform Resource Indentifier)用来标识服务器上的资源. URL:统一资源定位符(Uniform Resouce Locator)是资源标识符最 ...
- 远程访问数据库查询数据量一大就Hang
最近刚为客户升级了一套Oracle Database,一切进展顺利,眼看就要顺利验收时,发现有部分客户端软件连接新版本数据库时会Hang,问题非常诡异. 系统环境如下 升级前的环境OS:Windows ...
- 修改MyEclipse8.6的Servlet模板
D:\Genuitec\Common\plugins\com.genuitec.eclipse.wizards_****.jar 解压jar包, 进入templates文件夹,修改生成Servlet模 ...
- iOS开发那些事儿(四)the dark arts of the Objective-C runtime
一."Black Magic":Method Swizzling 利用 Runtime 特性把一个方法的实现与另一个方法的实现进行替换,也可以用runtime的四维理解——修改Di ...
- property参数
1,set方法内存管理相关的参数 *retain:release旧值,retain新值(适用于OC对象类型) *assign:直接赋值(默认,只用于非OC对象类型) *copy: release旧 ...
- C#修改 Excel指定单元格的值
/// <summary> /// 将指定字符串写入指定单元格中 /// </summary> /// <param name="data">要 ...
- Missing Number, First Missing Positive
268. Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find th ...