2017-03-28 17 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'はvoidメソッドですので、returnステートメントでそれを使用することはできません。 –

+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(); 
    } 
} 
関連する問題