Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Interface (Interface)


May 11, 2021 C#


Table of contents


Interface

Interfaces define the syntax contracts that should be followed when all classes inherit interfaces. The interface defines the "what is" section of the syntax contract, and the derived class defines the "How to" section of the syntax contract.

Interfaces define properties, methods, and events that are members of the interface. T he interface contains only the declarations of the members. M embers are defined as responsibilities of derived classes. Interfaces provide the standard structure that derived classes should follow.

Abstract classes are somewhat similar to interfaces, but they are mostly used only when only a few methods are declared by the base class to be implemented by derived classes.

Declare the interface

The interface uses the interface keyword declaration, which is similar to the class declaration. T he interface declaration is public by default. Here is an example of an interface declaration:

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

The following example demonstrates the implementation of the interface above:

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 produces the following results:

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

Precautions for interface use:

  1. Interface methods cannot be decorated with public abstracts, etc. There can be no field variables in the interface, constructors.
  2. Properties (methods with get and set) can be defined within the interface. S uch as string color s get; s et ; This kind of.
  3. When implementing an interface, it must be in the same format as the interface.
  4. All methods of the interface must be implemented.