Home  >  Article  >  Backend Development  >  [c# tutorial] C# interface (Interface)

[c# tutorial] C# interface (Interface)

黄舟
黄舟Original
2016-12-24 13:44:411422browse

C# Interface

Interface defines the syntax contract that all classes should follow when inheriting the interface. The interface defines the "what" part of the syntax contract, and the derived class defines the "how" part of the syntax contract.

Interface defines properties, methods and events, which are members of the interface. The interface only contains the declaration of members. Member definition is the responsibility of the derived class. Interfaces provide a standard structure that derived classes should follow.

Abstract classes are similar to interfaces to some extent, however, they are mostly used only when there are only a few methods declared by the base class and implemented by the derived class.

Declare interfaces

Interfaces are declared using the interface keyword, which is similar to the declaration of a class. Interface declarations are public by default. The following is an example of an interface declaration:

public interface ITransactions
{
   // 接口成员
   void showTransaction();
   double getAmount();
}

Example

The following example demonstrates the implementation of the above interface:

using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InterfaceApplication
{

   public interface ITransactions
   {
      // 接口成员
      void showTransaction();
      double getAmount();
   }
   public class Transaction : ITransactions
   {
      private string tCode;
      private string date;
      private double amount;
      public Transaction()
      {
         tCode = " ";
         date = " ";
         amount = 0.0;
      }
      public Transaction(string c, string d, double a)
      {
         tCode = c;
         date = d;
         amount = a;
      }
      public double getAmount()
      {
         return amount;
      }
      public void showTransaction()
      {
         Console.WriteLine("Transaction: {0}", tCode);
         Console.WriteLine("Date: {0}", date);
         Console.WriteLine("Amount: {0}", getAmount());

      }

   }
   class Tester
   {
      static void Main(string[] args)
      {
         Transaction t1 = new Transaction("001", "8/10/2012", 78900.00);
         Transaction t2 = new Transaction("002", "9/10/2012", 451900.00);
         t1.showTransaction();
         t2.showTransaction();
         Console.ReadKey();
      }
   }
}

When the above code is compiled and executed, it will produce the following results:

Transaction: 001
Date: 8/10/2012
Amount: 78900
Transaction: 002
Date: 9/10/2012
Amount: 451900

The above is [c# Tutorial】C# Interface (Interface) content, for more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn