如果一个方法返回一个接口,那意味着什么?

发布于 2024-08-24 00:41:24 字数 74 浏览 4 评论 0原文

我看到许多方法都指定接口作为返回值。我的想法是否正确,这意味着:我的方法可以返回从该接口继承的每个类类型?如果没有请给我一个好的答案。

I see many 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清风不识月 2024-08-31 00:41:24

是的,您的方法可以返回实现该接口的任何类型。

这是一个例子:

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, 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
    }
}
莳間冲淡了誓言ζ 2024-08-31 00:41:24

是的,这意味着您对返回的对象唯一了解的是它实现了接口。

事实上,调用代码甚至可能无法访问对象的实际类型。它可以是单独程序集中的私有类型。

事实上,该方法可能会从一次调用到下一次调用返回不同的类型(就像抽象工厂的情况一样)。

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).

不疑不惑不回忆 2024-08-31 00:41:24

是的,该方法可能会返回实现该接口的任何类型的对象。

但是,要使用特定类型的非接口成员,您需要将其强制转换为该类型。

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.

记忆消瘦 2024-08-31 00:41:24

C++ 支持一种称为多态性的编程技术。也就是说,对于其他对派生类一无所知的代码来说,派生类看起来就像基类。看一下他的示例:

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;
};

现在您可以从这段代码中看到,我们创建了一个基类 Shape(我们的接口)和两个专用于此类的派生类,一个是矩形,另一个是圆形。现在让我们创建一个打印形状面积的函数:

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

该函数不关心它是否是矩形的圆。或者它关心的是它传递了一个形状,并且无论类型如何,您都可以获得它的面积。

所以这段代码使用了这个函数:

 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

希望这有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文