接口做为参数传递,传递的是实现了接口的对象;
接口作为类型返回,返回的是实现了接口的对象。
using System;
// IShape接口
interface IShape
{
// Area属性
int Area
{
get;
set;
}
// Caculate计算方法
void Caculate();
}
// Circle类继承IShape
class Circle: IShape
{
// area字段
int area = 0;
// 构造函数
public Circle(int m_Area)
{
area = m_Area;
}
// Area属性
public int Area
{
get
{
return area;
}
set
{
area = value;
}
}
// Caculate方法
public void Caculate()
{
Console.WriteLine("计算面积!");
}
}
// MyClass类
class MyClass
{
// 构造函数
public MyClass(IShape shape)
{
shape.Caculate();
Console.WriteLine(shape.Area);
}
}
class Program
{
static void Main(string[]args)
{
//创建Circle类变量circle,并使用其作为参数创建MyClass型变量
myClass
Circle cir = new Circle(35);
MyClass myClass = new MyClass(cir);
Console.ReadLine();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。