我似乎遇到了界面问题。我有我的所有计算工作的贷款计划,但我似乎无法弄清楚如何调用我的界面。我敢肯定,这可能是我忽视的一些小问题,但由于某种原因,我有一个空白。
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
{
}
答案 0 :(得分:1)
这有帮助吗? See it working here.
using System;
public class Program
{
// Here we instantiate or construct a new instance of ThingWithMessage
// but we refer to it as thing of type IMyInterface,
// this works because ThingWithMessage implements IMyInterface.
// Then we use the interface implementation to get a message and
// write it to the console.
public static void Main()
{
IMyInterface thing = new ThingWithMessage();
Console.WriteLine(thing.GetMessage());
}
}
// This defines a type, or contract that classes can implement
interface IMyInterface
{
string GetMessage();
}
// This is a class that implements the IMyInterface interface
// effectively, it makes a promise to keep the contract
// specified by IMyInterface.
class ThingWithMessage : IMyInterface
{
public string GetMessage()
{
return "yes, this works.";
}
}
答案 1 :(得分:0)
这可能是你可以使用的东西:
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();
}
}