接口作为参数传递、返回
2014-04-29 16:56
183 查看
接口做为参数传递,传递的是实现了接口的对象;
接口作为类型返回,返回的是实现了接口的对象。
http://blog.csdn.net/tuziblog/article/details/4553922
接口的传递与返回就是围绕着上面的两句话展开的。
分别写两个例子来说明:
接口做为参数传递 :
[c-sharp] view
plaincopy
class Program
{
static void Main(string[] args)
{
Program p = new Program();
Man m = new Man(); //这里,想实现谁接口里的方法,就实例化谁,然后在下边就传谁
//Woman w = new Woman();
p.Get(w); //传递的是实现了接口的对象
Console.ReadLine();
}
public void Get(IPerson ipn) //接口做为参数传递
{
ipn.Say();
}
}
/// <summary>
/// 一个人类的接口
/// </summary>
public interface IPerson
{
void Say();
}
/// <summary>
/// 一个男人的类
/// </summary>
public class Man : IPerson
{
public void Say()
{
Console.WriteLine("我是一个男人");
}
}
/// <summary>
/// 一个女人的类
/// </summary>
public class Woman : IPerson
{
public void Say()
{
Console.WriteLine("我是一个女人");
}
}
接口做为参数返回 :
[c-sharp] view
plaincopy
class Program
{
static void Main(string[] args)
{
string a = Console.ReadLine();
Program pr = new Program();
pr.Getsay(a);
Console.ReadLine();
}
public Person Getsay(string a)
{
Person p = null;
switch (a)
{
case "1":
p = new Man();
p.Say();
break;
case "2":
p = new Woman();
p.Say();
break;
}
return p;
}
}
public interface Person
{
void Say();
}
public class Man : Person
{
public void Say()
{
Console.WriteLine("我是男人");
}
}
public class Woman : Person
{
public void Say()
{
Console.WriteLine("我是女人");
}
}
接口做为参数返回其实就是多态。
抽象类的传递和接口一样。不再说了
接口作为类型返回,返回的是实现了接口的对象。
http://blog.csdn.net/tuziblog/article/details/4553922
接口的传递与返回就是围绕着上面的两句话展开的。
分别写两个例子来说明:
接口做为参数传递 :
[c-sharp] view
plaincopy
class Program
{
static void Main(string[] args)
{
Program p = new Program();
Man m = new Man(); //这里,想实现谁接口里的方法,就实例化谁,然后在下边就传谁
//Woman w = new Woman();
p.Get(w); //传递的是实现了接口的对象
Console.ReadLine();
}
public void Get(IPerson ipn) //接口做为参数传递
{
ipn.Say();
}
}
/// <summary>
/// 一个人类的接口
/// </summary>
public interface IPerson
{
void Say();
}
/// <summary>
/// 一个男人的类
/// </summary>
public class Man : IPerson
{
public void Say()
{
Console.WriteLine("我是一个男人");
}
}
/// <summary>
/// 一个女人的类
/// </summary>
public class Woman : IPerson
{
public void Say()
{
Console.WriteLine("我是一个女人");
}
}
接口做为参数返回 :
[c-sharp] view
plaincopy
class Program
{
static void Main(string[] args)
{
string a = Console.ReadLine();
Program pr = new Program();
pr.Getsay(a);
Console.ReadLine();
}
public Person Getsay(string a)
{
Person p = null;
switch (a)
{
case "1":
p = new Man();
p.Say();
break;
case "2":
p = new Woman();
p.Say();
break;
}
return p;
}
}
public interface Person
{
void Say();
}
public class Man : Person
{
public void Say()
{
Console.WriteLine("我是男人");
}
}
public class Woman : Person
{
public void Say()
{
Console.WriteLine("我是女人");
}
}
接口做为参数返回其实就是多态。
抽象类的传递和接口一样。不再说了
相关文章推荐
- c#接口作为参数传递、返回
- c#接口作为参数传递、返回
- 怎么生成内部hide接口实例并通过反射作为参数进行传递
- C#接口作为传递参数
- C++中用struct来作为接口的传递参数
- CComPtr对象作为参数进行 1.值传递 2.引用传递 3.做为返回值的注意事项
- C函数只能有一个返回值,如果需要返回多个值,怎么办,其实很简单,只要将指针作为函数参数传递就可以了
- Java接口之作为参数传递
- 结构体作为参数的传递和返回
- Java中接口对象作为参数传递
- 给一个接口传递参数,并接收返回的参数
- 编写高质量代码改善C#程序的157个建议——建议97:优先考虑将基类型或接口作为参数传递
- 做JNI接口其中涉及到参数在C 和JAVA 之间的传递。
- SQL中函数返回表,传递一个参数,无法唤醒用户自定义函数,解决方法!
- springmvc学习笔记——传递对象作为参数(POJO)
- C语言中将二维数组作为函数参数来传递
- 业务层使用sql语句作为参数传递给永久层处理--mybatis的写法
- 指针作为函数的参数传递的问题
- 值 引用 指针作为参数传递
- 将Model实体类对象作为WebService接口参数(转)