2017-03-28 49 views
-1

我似乎遇到了界面問題。我有我的所有計算工作的貸款計劃,但我似乎無法弄清楚如何調用我的界面。我敢肯定,這可能是我忽視的一些小事,但由於某種原因,我有一個空白。貸款接口c程序故障#

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 
{ 

} 
+4

嗯,你的類不聲明它實現此刻的接口,而你根本沒有創建類的實例,或參考界面。另外,'Console.WriteLine'是一個無效的方法,所以你不能在像這樣的返回語句中使用它... –

+1

不管Jon Skeet如何說,都要把它作爲法則。 –

+0

或者,您可以在發佈之前嘗試調試您的代碼,而不必浪費Jon Skeet的時間。 – Jodrell

回答

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."; 
    } 
} 
+0

是的這個固定它。感謝您花時間幫助我。 – Shade

+0

@Shade,嘿,我只是想記起剛開始的樣子。 – Jodrell

+0

感謝您也解釋它。它使你更容易理解你的解釋。 – Shade

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(); 
    } 
}