C#Interface

黄舟
Release: 2017-02-06 16:31:14
Original
1540 people have browsed it

C# Interface

The 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 the interface

The interface is 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();
}
Copy after login

Example

The following example demonstrates the implementation of the above interface:

using System.Collections.Generic;using System.Linq;
using System.Text;using System;namespace InterfaceApplication{  
public interface ITransactions   {      
// interface members      
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();      
}   
}
}
Copy after login

When the above code is compiled and executed, it The following results will be produced:

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

The above is the content of the C# interface (Interface). For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template