1.38. C # interface

发布时间 : 2025-10-25 13:34:42 UTC      

Page Views: 9 views

Interface defines the syntax contract that all classes should follow when inheriting interfaces. The interface defines the “what is” part of the grammar contract, and the derived class defines the “how to do” part of the grammar contract.

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

The interface makes the class or structure that implements the interface consistent in form.

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

The interface itself does not implement any function, it just enters into a contract with the object that declares to implement the interface what behavior must be implemented.

Abstract classes cannot be instantiated directly, but allow concrete, functional classes to be derived.

1.38.1. Define the interface: MyInterface.cs #

Interface usage interface keyword declaration, which is similar to a class declaration. The interface declaration defaults to public . Here is an example of an interface declaration:

interface IMyInterface { void MethodToImplement(); } 

The above code defines the interface IMyInterface . Usually the interface command begins with the letter I, and there is only one method forthis interface MethodToImplement() , no parameters and return values, but of course we can set parameters and return values as required.

It is worth noting that there is no concrete implementation of this method.

1.38.2. Implement the above interfaces: InterfaceImplementer.cs #

Example

using System; interface IMyInterface { // Interface Members void MethodToImplement(); } class InterfaceImplementer : IMyInterface { static void Main() { InterfaceImplementer iImp = new InterfaceImplementer(); iImp.MethodToImplement(); } public void MethodToImplement() { Console.WriteLine("MethodToImplement() called."); } } 

InterfaceImplementer class implements the IMyInterface interface, the implementation of the interface is similar to the inheritance syntax of the class:

class InterfaceImplementer : IMyInterface 

After inheriting the interface, we need to implement the method of the interface MethodToImplement() method name must match the method name defined by the interface

1.38.3. Interface inheritance: InterfaceInheritance.cs #

The following example defines two interfaces IMyInterface and IParentInterface .

If one interface inherits from other interfaces, then the implementation class or structure needs to implement the members of all interfaces.

The following example IMyInterface inherited IParentInterface interface, so the interface implementation class must implement the MethodToImplement() and ParentInterfaceMethod() methods:

Example

using System; interface IParentInterface { void ParentInterfaceMethod(); } interface IMyInterface : IParentInterface { void MethodToImplement(); } class InterfaceImplementer : IMyInterface { static void Main() { InterfaceImplementer iImp = new InterfaceImplementer(); iImp.MethodToImplement(); iImp.ParentInterfaceMethod(); } public void MethodToImplement() { Console.WriteLine("MethodToImplement() called."); } public void ParentInterfaceMethod() { Console.WriteLine("ParentInterfaceMethod() called."); } } 

The output result of the instance is:

MethodToImplement() called. ParentInterfaceMethod() called. 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。