1.38. C # interface

发布时间 :2023-10-12 23:00:06 UTC      

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.

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.