开发者

If a method returns an interface, what does it mean?

开发者 https://www.devze.com 2022-12-21 19:04 出处:网络
I see ma开发者_开发问答ny methods that specify an interface as return value. Is my thought true that it means: my method can return every class type that inherits from that interface? if not please gi

I see ma开发者_开发问答ny methods that specify an interface as return value. Is my thought true that it means: my method can return every class type that inherits from that interface? if not please give me a good answer.


Yes, your method could return any type that implements that interface.

Here is an example:

using System;

class Foo
{
    public IComparable GetComparable()
    {
        // Either of these return statements
        // would be valid since both System.Int32
        return 4;
        // and System.String
        return "4";
        // implement System.IComparable
    }
}


Yes, it means that the only thing you know about the object that is returned is that it implements the interface.

In fact, the actual type of the object may not even be accessible to the calling code. It could be a private type in a separate assembly.

And in fact, the method may return a different type from one invocation to the next (as in the case of an abstract factory).


Yes, that method might return an object of any type that implements that interface.

But, to use the non-interface members of a particular type, you'll need to cast it to that type.


C++ supports a programming technique called polymorphism. That is a derived class can look like a base class to other code that knows nothing about the derived classes. Take a look at his example:

class Shape
{
public:
    virtual float Area () const = 0;
};

class Rectangle: public Shape
{
public:
    Rectangle (float width, float height)
        : m_width(width)
        , m_height(height)
    {}

    virtual float Area () const
    {
        return m_width * m_height;
    }

private:
    float m_width;
    float m_height;
};

class Circle: public Shape
{
public:
    Circle (float radius)
        : m_radius(radius)
    {}

    virtual float Area () const
    {
        return 3.141592653f*(m_radius*m_radius);
    }

private:
    float m_radius;
};

Now you can see from this code we've created a base class Shape (our interface) and two derived classes that specialise this class, one a rectangle, another a circle. Now let's create a function that prints out the area of a shape:

void PrintArea (const Shape& shape)
{
    printf("Area of shape = %f",shape.Area());
}

This function doesn't care if its a circle of rectangle. Or it cares about is that it's passed a shape and that you can get the area of it, regardless of type.

So this code uses this function:

 Rectangle r (5.0f,4.0f);
 Circle c (25.0f);

 PrintArea(r);      // Print the area of the rectangle
 PrintArea(c);      // Print the area of the circle

Hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号