What is an empty interface used for

后端 未结 12 2736
醉话见心
醉话见心 2020-12-13 17:18

I am looking at nServiceBus and came over this interface

namespace NServiceBus
{
    public interface IMessage
    {
    }
}

What is the us

12条回答
  •  孤街浪徒
    2020-12-13 17:56

    An empty interface acts simply as a placeholder for a data type no better specified in its interface behaviour.

    In Java, the mechanism of the interface extension represents a good example of use. For example, let's say that we've the following

    interface one {}
    interface two {}
    
    interface three extends one, two {}
    

    Interface three will inherit the behaviour of 'one' and 'two', and so

    class four implements three { ... }
    

    has to specify the two methods, being of type 'three'.

    As you can see, from the above example, empty interface can be seen also as a point of multiple inheritance (not allowed in Java).

    Hoping this helps to clarify with a further viewpoint.

提交回复
热议问题