提示是这个问题:“A.Me”不实现接口成员“A.Ibankio.have”。“A.Me.have”无法实现接口成员,因为它不是公共的
请教各位大神,这是为什么呢?using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace A
{
interface Ibankio
{
decimal have //已有钱数
{
get;
set;
}
void In(decimal a);//存入钱数;
void Out(decimal b);//支出钱数;
/*decimal c//剩余钱数
{
get;
set; }*/
} class Me:Ibankio
{
private decimal have
{
get
{
return have;
}
set
{
if (have < 0)
{
Console.WriteLine("卡内余额不足");
}
}
}
public void In(decimal a)
{
have += a;
Console.WriteLine("您账户里面现有{0}元,您存了{1}元钱", have, a);
}
public void Out(decimal b)
{
if (b > have)
{
have -= b;
Console.WriteLine("您账户支出{0}元,剩余{1}元!", b, have);
}
else
Console.WriteLine("余额不足!");
}
/*public decimal c
{
get
{
return c;
}
set
{
if (have >= 0)
{
value = have;
c = value;
}
}
}*/
} class Program
{
static void Main(string[] args)
{
Ibankio wo = new Me();
wo.have = 1000;
wo.In(10000);
wo.Out(12000); }
}
}
请教各位大神,这是为什么呢?using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace A
{
interface Ibankio
{
decimal have //已有钱数
{
get;
set;
}
void In(decimal a);//存入钱数;
void Out(decimal b);//支出钱数;
/*decimal c//剩余钱数
{
get;
set; }*/
} class Me:Ibankio
{
private decimal have
{
get
{
return have;
}
set
{
if (have < 0)
{
Console.WriteLine("卡内余额不足");
}
}
}
public void In(decimal a)
{
have += a;
Console.WriteLine("您账户里面现有{0}元,您存了{1}元钱", have, a);
}
public void Out(decimal b)
{
if (b > have)
{
have -= b;
Console.WriteLine("您账户支出{0}元,剩余{1}元!", b, have);
}
else
Console.WriteLine("余额不足!");
}
/*public decimal c
{
get
{
return c;
}
set
{
if (have >= 0)
{
value = have;
c = value;
}
}
}*/
} class Program
{
static void Main(string[] args)
{
Ibankio wo = new Me();
wo.have = 1000;
wo.In(10000);
wo.Out(12000); }
}
}
interface Ibankio
==>
public interface Ibankio
private decimal have
==>
public decimal have
如果需要使用 private,那么使用 Interface.Method(...) 实现之 。
void Out(decimal b);//支出钱数;为明确标识前缀的都默认为private
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Error
{
interface Ibankio
{
decimal have //已有钱数
{
get;
set;
}
void In(decimal a);//存入钱数;
void Out(decimal b);//支出钱数;
/*decimal c//剩余钱数
{
get;
set;
}*/ } class Me : Ibankio
{
private static decimal count;
public decimal have
{
get
{
return count;
}
set
{
if (count < 0)
{
Console.WriteLine("卡内余额不足");
} }
}
public void In(decimal a)
{
count += a;
Console.WriteLine("您账户里面现有{0}元,您存了{1}元钱", have, a);
}
public void Out(decimal b)
{
if (b < count)
{
count -= b;
Console.WriteLine("您账户支出{0}元,剩余{1}元!", b, have);
}
else
Console.WriteLine("余额不足!");
}
/*public decimal c
{
get
{
return c;
}
set
{
if (have >= 0)
{
value = have;
c = value;
}
}
}*/
} class Program
{
static void Main(string[] args)
{
Ibankio wo = new Me();
wo.have = 1000;
wo.In(10000);
wo.Out(12000); }
}
}
{
get
{
return have;
}
set
{
if (have < 0)
{
Console.WriteLine("卡内余额不足");
}
}
这段代码总觉得有问题,我只是通过经验修改的,要说为什么你只能自己体会了,我只会改不会说,我的理论知识很薄
namespace A
{
interface Ibankio
{
decimal have //已有钱数
{
get;
set;
}
void In(decimal a);//存入钱数;
void Out(decimal b);//支出钱数;
} class Me : Ibankio
{
private decimal _hava;
public decimal have
{
get { return _hava; }
set
{
if (value < 0)
{
Console.WriteLine("卡内余额不足");
}
else
{
_hava = value;
}
}
}
public void In(decimal a)
{
have += a;
Console.WriteLine("您账户里面现有{0}元,您存了{1}元钱", have, a);
}
public void Out(decimal b)
{
if (b > have)
{
have -= b;
Console.WriteLine("您账户支出{0}元,剩余{1}元!", b, have);
}
else
Console.WriteLine("余额不足!");
}
} class Program
{
static void Main(string[] args)
{
Ibankio wo = new Me();
wo.have = 1000;
wo.In(10000);
wo.Out(12000); Console.ReadLine();
}
}
}
互帮互助!
不过,能不能帮我看一下,我那个in方法里面have+=a的这个过程为啥不能实现呢?
就是不能把have原来的值加进去!
互帮互助!
不过,能不能帮我看一下,我那个in方法里面have+=a的这个过程为啥不能实现呢?
就是不能把have原来的值加进去!
把你的代码贴出来,就目前的代码来说,程序是一次性的,你是想做成那种有输入有输出的吧
{
get
{
return count;
}
set
{
if (count < 0)
{
Console.WriteLine("卡内余额不足");
}
count = value;
}
}
我的错误,呵呵,这样就行了,不过你的代码很需要优化
互帮互助!
不过,能不能帮我看一下,我那个in方法里面have+=a的这个过程为啥不能实现呢?
就是不能把have原来的值加进去!
把你的代码贴出来,就目前的代码来说,程序是一次性的,你是想做成那种有输入有输出的吧
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace A
{
public interface Ibankio
{
decimal have //已有钱数
{
get;
set;
}
void In(decimal a);//存入钱数;
void Out(decimal b);//支出钱数;
/*decimal c//剩余钱数
{
get;
set; }*/
} class Me:Ibankio
{
private decimal Have;
public decimal have
{
get
{
return Have;
}
set
{
if (value < 0)
{
Console.WriteLine("卡内余额不足");
}
else
Have = value;
}
}
public void In(decimal a)
{
have += a;
Console.WriteLine("您账户里面现有{0}元,您存了{1}元钱", have, a);
}
public void Out(decimal b)
{
if (b > have)
{
have -= b;
Console.WriteLine("您账户支出{0}元,剩余{1}元!", b, have);
}
else
Console.WriteLine("余额不足!");
}
/*public decimal c
{
get
{
return c;
}
set
{
if (have >= 0)
{
value = have;
c = value;
}
}
}*/
} class Program
{
static void Main(string[] args)
{
Ibankio wo = new Me();
wo.have = 1000;
wo.In(10000);
wo.Out(12000); }
}
}是的,就是想做成那种有输入有输出的模式...而且我做这个好像根本不对....哎,头疼.
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;namespace Error
{
interface Ibankio
{
decimal have //已有钱数
{
get;
set;
}
void In(decimal a);//存入钱数;
void Out(decimal b);//支出钱数;
} class Me : Ibankio
{
private static decimal count;
public decimal have
{
get
{
return count;
}
set
{
if (count < 0)
{
Console.WriteLine("卡内余额不足");
}
count = value;
}
}
public void In(decimal a)
{
count += a;
Console.WriteLine("您账户里面现有{0}元,您存了{1}元钱", have, a);
}
public void Out(decimal b)
{
if (b < count)
{
count -= b;
Console.WriteLine("您账户支出{0}元,剩余{1}元!", b, have);
}
else
Console.WriteLine("余额不足!");
}
/*public decimal c
{
get
{
return c;
}
set
{
if (have >= 0)
{
value = have;
c = value;
}
}
}*/
} class Program
{
static void Main(string[] args)
{
Ibankio wo = new Me();
int amount = 0;
bool flag = true;//标志位,主要控制用户输入信息是否正确
string errorinput1="\\D";
string order="";
wo.have = 1000;//设置初始值
Console.WriteLine("------------------------欢迎使用自助存取款机!---------------------");
while (true)
{
while (flag)
{
Console.WriteLine("请选择操作:存钱请输入D,取钱请输入W,退出请输入Q");
string temp = Console.ReadLine();
if (temp.Equals("D") || temp.Equals("d") || temp.Equals("W") || temp.Equals("w") || temp.Equals("Q") || temp.Equals("q"))
{ flag = false; order = temp; }
else
Console.WriteLine("请检查您的输入:您输入的是{0},应为‘W’‘D’‘Q’中的一个!",temp);
}
flag = true;
if (order.Equals("Q")||order.Equals("q"))
break;
Console.WriteLine();
while (flag)
{
Console.WriteLine("请输入金额并按回车键结束:");
string temp = Console.ReadLine();
flag=Regex.IsMatch(temp,errorinput1);
if (!flag)
amount = Convert.ToInt32(temp);
else
Console.WriteLine("您输入的是{0},应为数字!",temp);
}
flag = true;
if (order.Equals("D") || order.Equals("d"))
wo.In(amount);
else if (order.Equals("W") || order.Equals("w"))
wo.Out(amount);
}
Console.WriteLine("谢谢使用,再见!");
}
}
}
{
get
{
return have;
}
set
{
if (have < 0)
{
Console.WriteLine("卡内余额不足");
}
}
}
这段很有问题,对于接口实现,接口里面不需要写权限,都是public,实现接口,如果不了解的话,VS环境,右键点击class Me : Ibankio的Ibankio,选择【重构】-【实现接口】,然后再在里面写代码吧。
这里是要用public的,另外这里的set都没做赋值处理,你要有个变量来接收,get的时候再return该变量,同时属性是建议你首字母大写的,于是变成这样:private decimal _have;
public decimal Have
{
get
{
return _have;
}
set
{
_have=value;
if (_have < 0)
{
Console.WriteLine("卡内余额不足");
}
}
}
如果自学的话,建议你看《你不知道的.net》