C # interface (Interface)
The interface defines the syntax contracts that should be followed when all classes inherit an interface. The interface defines the "what" part of the syntax contract, and the derived class defines the "How to" section of the syntax contract.
An interface defines properties, methods, and events, which are members of an interface. The interface contains only the declarations of the members. The definition of a member is the responsibility of the derived class. The interface provides the standard structure that a derived class should follow.
Abstract classes are somewhat similar to interfaces, but most of them are only used when only a few methods are declared by the base class by a derived class.
declaring interfaces
An interface uses the interface keyword declaration, which is similar to a class declaration. The interface declaration is public by default. The following is an instance of an interface declaration:
public interface itransactions{ //interface member void Showtransaction (); Double Getamount ();}
Instance
The following example shows the implementation of the above interface:
Using system.collections.generic;using system.linq;using system.text;namespace interfaceapplication{Public Interface Itransactions {//interface member 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:001date:8/10/2012amount:78900transaction:002date:9/10/2012amount:451900
The above is the "C # Tutorial" C # Interface (Interface) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!