C# - 接口的继承
2024-10-19 03:33:16
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 接口
{
/// <summary>
/// 接口:定义一个统一的标准
/// 声明接口,接口中,只包含成员的生命,不包含任何的代码实现。
/// 接口成员总是公有的,不添加也不需要添加public,也不能声明虚方法和虚静态方法
/// </summary>
interface IBankAccount
{
//方法
void PayIn(decimal amount); //方法
bool WithShowMyself(decimal amount); //属性
decimal Balance { get; }
} /// <summary>
/// 继承自IBankAccount的接口
/// </summary>
interface ITransferBankAccount : IBankAccount
{
//转账 :转入的目的地 :转入金额
bool TransferTo(IBankAccount destination, decimal amount);
} class SaveAcount : IBankAccount
{
//私有变量
private decimal banlance; //存款
public void PayIn(decimal amount)
{
banlance += amount;
} //取款
public bool WithShowMyself(decimal amount)
{
if (banlance >= amount)
{
banlance -= amount;
return true;
}
else
{
Console.WriteLine("余额不足!");
return false;
}
} //账户余额
public decimal Balance
{
get
{
return banlance;
}
}
} //实现接口的类的相应成员必须添加public修饰
class ITransferAccount : ITransferBankAccount
{
//私有变量
private decimal banlance; //存款
public void PayIn(decimal amount)
{
banlance += amount;
} //取款
public bool WithShowMyself(decimal amount)
{
if (banlance >= amount)
{
banlance -= amount;
return true;
}
else
{
Console.WriteLine("余额不足!");
return false;
}
} //账户余额
public decimal Balance
{
get
{
return banlance;
}
} //转账
public bool TransferTo(IBankAccount destination, decimal amount)
{
bool result = WithShowMyself(amount); if (result == true)
{
destination.PayIn(amount);
} return result;
}
} class Program
{
static void Main(string[] args)
{
IBankAccount MyAccount = new SaveAcount(); ITransferAccount YourAccount = new ITransferAccount(); MyAccount.PayIn(); YourAccount.PayIn(); YourAccount.TransferTo(MyAccount, ); Console.WriteLine(MyAccount.Balance);//
Console.WriteLine();
Console.WriteLine(YourAccount.Balance);// Console.ReadKey();
}
}
}
最新文章
- php : 自定义分页类
- (C# &; Unity) 脚本语言 ES
- jQuery基础之选择器
- 跟着鸟哥学Linux系列笔记3-第11章BASH学习
- SAP iDoc 概念及管理
- LeetCode 笔记系列 19 Scramble String [合理使用递归]
- StrutsPrepareAndExecuteFilter的作用
- matlab添加M_map工具箱(转 http://blog.sina.com.cn/s/blog_491b86bf0100srt9.html)
- 2014辛星完全解读html第五节
- C#研究OpenXML之路(1-新建工作簿文件)
- Vivo展柜灯怎样设计才吸引大量客户?
- Azure 认知服务 (5) 计算机视觉API - 使用C#代码实现读取图片中的文字(OCR)功能
- 使用telnet发送HTTP请求
- lua continue实现
- java testng框架的windows自动化-自动运行testng程序下篇
- Vc数据库编程基础1
- Oracle12c Data Guard搭建手册
- Ogre 监听类与渲染流程
- Laravel5.1 填充数据库
- vue 局部引入js插件
热门文章
- EBS动态创建账户组合实现
- asp.net从客户端检测到有潜在危险的Request.Form 值
- 绫致时装讲述O2O细节:野心在“私人定制” - 移动购物 - 亿邦动力网
- poj 3691
- c++,static 静态成员变量 / 静态成员函数
- 机器学习笔记(一)- from Andrew Ng的教学视频
- java.lang.NoClassDefFoundError: org.ksoap2.transport.HttpTransportSE异常处理
- PHP学习笔记2-流程控制
- Vue.js介绍
- 一致性算法--Paxos