C# 贷款程序与接口c的故障#
我似乎在界面方面遇到了问题。我有它,我所有的计算工作为贷款计划,但我似乎不知道如何调用我的接口。我肯定这可能是我忽略了一些次要的东西,但出于某种原因,我有一个空白C# 贷款程序与接口c的故障#,c#,interface,C#,Interface,我似乎在界面方面遇到了问题。我有它,我所有的计算工作为贷款计划,但我似乎不知道如何调用我的接口。我肯定这可能是我忽略了一些次要的东西,但出于某种原因,我有一个空白 Interface: interface IMyInterface { string iMessage(); } public class C1 { static void Main(string[] args) { double interests = 0.0; doub
Interface:
interface IMyInterface
{
string iMessage();
}
public class C1
{
static void Main(string[] args)
{
double interests = 0.0;
double years = 0.0;
double loan_amount = 0.0;
double interest_rate = 0.0;
Console.Write("Enter Loan Amount:$ ");
loan_amount = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter Number of Years: ");
years = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter Interest Rate: ");
interest_rate = Convert.ToDouble(Console.ReadLine());
interests = loan_amount * interest_rate * years;
Console.WriteLine("\nThe total interests is {0}", interests);
Console.ReadLine();
}
public string iMessage()
{
return Console.WriteLine("Be Ready!");
}
}
class Program
{
}
这有用吗
这可能是您可以使用的:
interface IMyInterface
{
double Calculate();
}
class MyCalculationLogics : IMyInterface
{
public double Calculate(double loan_amount, double years, double interest_rate)
{
return loan_amount * interest_rate * years;
}
}
public class Program
{
static void Main(string[] args)
{
....
// Get the values from the user
....
IMyInterface myCalc = new MyCalculationLogics();
interests = myCalc.Calculate(loan_amount, years, interest_rate);
Console.WriteLine("\nThe total interests is {0}", interests);
Console.ReadLine();
}
}
你的类目前没有声明它实现了接口,你也从来没有创建过你的类的实例,或者引用过接口。另外,
Console.WriteLine
是一个无效的方法,所以你不能在像这样的返回语句中使用它……Shade,换句话说,不管Jon Skeet说什么,都把它当作法律。或者,你可以在发布之前试着调试你的代码,永远不必浪费Jon Skeet的时间。@JonSkeet好的,谢谢你指出这一点。我很感激。当你说界面时,你是指控制台应用程序中的用户界面吗?哪个请求输入并给出输出?=!是的,这个修好了。谢谢你花时间来帮助我。@Shade,嘿,我几乎还记得刚开始时的感觉。谢谢你也解释清楚了。你的解释使它更容易理解。
interface IMyInterface
{
double Calculate();
}
class MyCalculationLogics : IMyInterface
{
public double Calculate(double loan_amount, double years, double interest_rate)
{
return loan_amount * interest_rate * years;
}
}
public class Program
{
static void Main(string[] args)
{
....
// Get the values from the user
....
IMyInterface myCalc = new MyCalculationLogics();
interests = myCalc.Calculate(loan_amount, years, interest_rate);
Console.WriteLine("\nThe total interests is {0}", interests);
Console.ReadLine();
}
}