建立一个接口IBank,包含取款存款和查询功能,实例化对象,模拟简单的银行存取款过程。using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
interface IBank
{
void QuKuan;
void CunKuan;
void ChaXun();}class MyBank : IBank
{
int x = 1000;
public MyBank(int y);
public int QuKuan(int y)
{
Console.WriteLine("取款:");
int a = Console.Read();
x = x - a;
if (x < 0)
{
Console.WriteLine("余额不足!");
}
return x; }
public int CunKuan(int y)
{
Console.WriteLine("存款:");
int b = Console.Read();
x = x + b;
Console.WriteLine("存款 :{0},b");
return x; }
public int ChaXun(int y)
{
Console.WriteLine("余额为:{0},x");
return x;
}
class Account
{ public void Main()
{ Console.WriteLine("请选择 :1=取款 2=存款 3=查询");
int y = Console.Read();
MyBank bank = new MyBank(y);
switch (y)
{
case 1:
bank.QuKuan(y);
break;
case 2:
bank.CunKuan(y);
break;
case 3:
bank.ChaXun(y);
break; }
}
}}运行后出现了“ 接口不能包含字段 9 10”错误,请高手指教,谢!
using System.Collections.Generic;
using System.Linq;
using System.Text;
interface IBank
{
void QuKuan;
void CunKuan;
void ChaXun();}class MyBank : IBank
{
int x = 1000;
public MyBank(int y);
public int QuKuan(int y)
{
Console.WriteLine("取款:");
int a = Console.Read();
x = x - a;
if (x < 0)
{
Console.WriteLine("余额不足!");
}
return x; }
public int CunKuan(int y)
{
Console.WriteLine("存款:");
int b = Console.Read();
x = x + b;
Console.WriteLine("存款 :{0},b");
return x; }
public int ChaXun(int y)
{
Console.WriteLine("余额为:{0},x");
return x;
}
class Account
{ public void Main()
{ Console.WriteLine("请选择 :1=取款 2=存款 3=查询");
int y = Console.Read();
MyBank bank = new MyBank(y);
switch (y)
{
case 1:
bank.QuKuan(y);
break;
case 2:
bank.CunKuan(y);
break;
case 3:
bank.ChaXun(y);
break; }
}
}}运行后出现了“ 接口不能包含字段 9 10”错误,请高手指教,谢!
{
void QuKuan();
void CunKuan();
void ChaXun(); } ???
interface IBank
{
int QuKuan(int y);
int CunKuan(int y);
int ChaXun(int y) ;
}
你这样的情况最好用抽象类来作为继承的根而不是接口。
接口隔离原则(Interface Segregation Principle)讲的是:使用多个专门的接口比使用单一的总接口总要好。换而言之,从一个客户类的角度来讲:一个类对另外一个类的依赖性应当是建立在最小接口上的。
void CunKuan; 这里没括号
接口里面是不能定义字段的....
int Value { get; }