当前位置: 编程技术>.net/c#/asp.net
有关c# 接口的一个简单实例
来源: 互联网 发布时间:2014-08-30
本文导语: c#接口实例代码。 1,第一部分 代码示例: namespace ConsoleApplication1 { using System; using System.Collections.Generic; using System.Text; public class BankMethod : IBankAccount { decimal balance; public void PayIn(decimal Account) { balance += Account; //Console.Wri...
c#接口实例代码。
1,第一部分
代码示例:
namespace ConsoleApplication1
{
using System;
using System.Collections.Generic;
using System.Text;
public class BankMethod : IBankAccount
{
decimal balance;
public void PayIn(decimal Account)
{
balance += Account;
//Console.WriteLine("您现在的存款是:{0}",balance);
}
public bool PayOut(decimal Account)
{
if (Balance > Account)
{
balance -= Account;
Console.WriteLine("您已经取走了{0},还剩下余额是:{1}", Account, balance);
return true;
}
Console.WriteLine("提款失败!");
return false;
}
public decimal Balance
{
get { return balance; }
}
public override string ToString()
{
return string.Format("您现在的存款是:{0:C}", balance);
}
}
class Test
{
static void Main()
{
IBankAccount Huguo = new BankMethod();
IBankAccount guo = new BankMethod();
Huguo.PayIn(10000);
guo.PayIn(200000);
Console.WriteLine(Huguo.ToString());
Console.WriteLine(guo.ToString());
//BankMethod Bank = new BankMethod();
//Bank.PayIn(200000);
//Bank.PayOut(30000);
}
}
}
{
using System;
using System.Collections.Generic;
using System.Text;
public class BankMethod : IBankAccount
{
decimal balance;
public void PayIn(decimal Account)
{
balance += Account;
//Console.WriteLine("您现在的存款是:{0}",balance);
}
public bool PayOut(decimal Account)
{
if (Balance > Account)
{
balance -= Account;
Console.WriteLine("您已经取走了{0},还剩下余额是:{1}", Account, balance);
return true;
}
Console.WriteLine("提款失败!");
return false;
}
public decimal Balance
{
get { return balance; }
}
public override string ToString()
{
return string.Format("您现在的存款是:{0:C}", balance);
}
}
class Test
{
static void Main()
{
IBankAccount Huguo = new BankMethod();
IBankAccount guo = new BankMethod();
Huguo.PayIn(10000);
guo.PayIn(200000);
Console.WriteLine(Huguo.ToString());
Console.WriteLine(guo.ToString());
//BankMethod Bank = new BankMethod();
//Bank.PayIn(200000);
//Bank.PayOut(30000);
}
}
}
2,第二部分
代码示例:
namespace ConsoleApplication1
{
public interface IBankAccount
{
void PayIn(decimal amount);
bool PayOut(decimal amount);
decimal Balance
{
get;
}
}
public interface IBankTransfer:IBankAccount
{
bool Transfer(IBankAccount Action,decimal amount);
}
}
{
public interface IBankAccount
{
void PayIn(decimal amount);
bool PayOut(decimal amount);
decimal Balance
{
get;
}
}
public interface IBankTransfer:IBankAccount
{
bool Transfer(IBankAccount Action,decimal amount);
}
}