本文介绍了如果一个方法返回一个接口,那是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我看到许多将接口指定为返回值的方法.我的想法是否正确,是否意味着:我的方法可以返回从该接口继承的每个类类型?如果没有,请给我一个很好的答案.
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.
推荐答案
是的,您的方法可以返回实现该接口的任何类型.
Yes, your method could return any type that implements that interface.
这里是一个例子:
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
}
}
这篇关于如果一个方法返回一个接口,那是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!