C#中使用接口作参数(新手问题)

bg8crv 2007-03-16 10:47:50
using System;
using System.Collections.Generic;
using System.Text;

namespace Example10_8
{
class program
{
public static void Main(string[] args)
{
Circle circle = new Circle(35);
MyClass myClass = new MyClass(circle);

Console.ReadLine();
}

}

//ISshape接口
interface ISshape
{
//ISshape属性
int Area
{
get;
set;
}
//ISshape方法
void Caculate();
}
//Circle类继承ISshape
class Circle : ISshape
{
//初始化字段
int area = 0;

//构造函数
public Circle(int m_Area)
{
area = m_Area;
}

#region ISshape成员

//Area属性
public int Area
{
get
{
return area;
}
set
{
area = value;
}
}
//Caculate方法
public void Caculate()
{
Console.WriteLine("计算面积");
}
#endregion
}
//MyClass类
class MyClass
{
public MyClass(ISshape shape)
{
shape.Caculate();

Console.WriteLine(shape.Area);
}
}
}


/*
小弟最近正在苦读C#,看到了接口这儿,有点晕了
请问这段程序中的“35”这个值,是怎么传递过去的呢?
其实说白了,就是想请教一下大家这个接口到底是怎么个继承的关系哈!~
谢谢!
*/
...全文
2628 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
li_oxen 2007-03-16
  • 打赏
  • 举报
回复
d
北京的雾霾天 2007-03-16
  • 打赏
  • 举报
回复
实际是把circle看做ISshape类型来处理的。

因为circle有Area属性,是从ISshape继承来的。
所以可以直接按ISshape 来处理;

接口就是一种约定,实现了接口,就是实现了这个接口的约定,也就一定有接口的成员,也就可以通过这个约定调用接口中有的成员。
bg8crv 2007-03-16
  • 打赏
  • 举报
回复
谢谢哈!

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧