什么是接口?
在编程中有"接口"的概念,请问什么是接口,为什么使用要接口,平常的方法不能解决问题,一定要使用接口吗?什么地方需要用到接口,能不能提供一些使用接口的简单实例吗,然后说一说接口能解决什么具体问题?谢谢!如果哪位能提供"FIF"小组的有关了的VIP FLASH视频下载吗?
程序代码:using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
public interface IshapeFunction //定义一个接口;
{
double GetArea();//定义一个没有实现只有声明的方法,
}
class circle : IshapeFunction //circle继承自IshapeFunction接口,他必须实现IshapeFunction接口中的GetArea()方法;
{
private double radius; //定义半径
public circle(double rad) //初始化半径
{
radius = rad;
}
public double GetArea() //实现接口中的方法
{
return (Math.PI * radius * radius); //求出面积
}
public string ShowMe() //显示自己
{
return ("Circle");
}
}
class Rectangle : IshapeFunction //Rectangle继承自IshapeFunction接口,他必须实现IshapeFunction接口中的GetArea()方法;
{
private double width, height;//定义长和宽
public Rectangle(double myWidth, double myHeight)//初始化长宽
{
width = myWidth;
height = myHeight;
}
public double GetArea() //求出面积
{
return (width * height);
}
public string ShowMe() //显示自己
{
return ("Rectangle");
}
}
class Program
{
static void Main(string[] args)
{
circle myCircle = new circle(4); //声明一个circle对象
IshapeFunction myICircle = myCircle;//创建一个变量,它引用了这个circle对象,不过,由于这个变量指定为IshapeFunction类型,所以它只能访问该接口的成员。这就是所谓的“接口类型”,通过用接口类型,可以有效的创建过滤器,从而限制只能对接口成员进行访问。
Rectangle myRectangle = new Rectangle(4, 8);//声明一个Rectangle对象。
IshapeFunction[] myShapes ={ myCircle, myRectangle };//使用接口最大的好处之一在于,程序员可以用相同的方式处理不同的类,只要它们实现相同的接口就可以,下面演示这个功能。
Console.WriteLine(myShapes[0].GetArea().ToString());
Console.WriteLine(myShapes[1].GetArea().ToString());
Console.WriteLine(myCircle.GetArea().ToString()); //打印mycircle的面积
Console.WriteLine(myRectangle.GetArea().ToString());//打印myrectangle的面积
Console.WriteLine(myCircle.ShowMe());
Console.WriteLine(myRectangle.ShowMe());
Console.WriteLine(myICircle.GetArea().ToString());
// Console.WriteLine(myICircle.ShowMe()); 上面的可以通过,下面提示出错,因为通过接口类型创建了过滤器,限制只能对接口成员进行访问。
System.Threading.Thread.Sleep(100000);
}
}
}
程序代码: public class ShapeUtil
{
public static double SumAreas(IshapeFunction[] funArray)
{
double tot = 0.0;
for (int i = 0; i < funArray.Length; i++)
{
tot += funArray[i].GetArea();
}
return tot;
}
}
程序代码:circle myCircle = new circle(4); //声明一个circle对象
IshapeFunction myICircle;
myICircle = myCircle as IshapeFunction;
if (myICircle != null)
{ }
circle myCircle2 = new circle(5.0);
if (myCircle2 is IshapeFunction)
{ } public double IshapeFunction.GetArea()
{
……
}
程序代码: object[] myStack = new object[50];
myStack[0] = new circle(5.0); //将circle对象保存到数组
myStack[1] = "Circle"; //将string对象放到数组中
circle c1 = myStack[0] as circle;
if (c1 != null)
{
circle c2 = (circle)myStack[1];
}
public class myCollection<T>
{
T[] myStack = new T[50];
}myCollection<string> mc=new myCollection<string> ();//string是类型参数,它指定了这个类只处理string类型
public class myCollection<T> where T:IComparable
{
T[] myStack = new T[50];
}
程序代码:using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication2
{
public class GenStack<T> //定义一个范型类,参数T是受限的,它必须是引用类型。
where T : class
{
private T[] stackCollection; //范型数组
private int count = 0;
public GenStack(int size) //构造函数
{
stackCollection = new T[size]; //初始化参数
}
public void Add(T item) //接受范型参数的方法
{
stackCollection[count] = item;
count += 1;
}
public T this[int ndx] //定义一个索引器来参数化成员属性
{
get
{
if (!(ndx < 0 || ndx > count - 1))
{
return stackCollection[ndx];
}
else
{
return (default(T));
}
}
}
public int ItemCount
{
get
{
return count;
}
}
public int Compare<C>(T value, T value2)
{
return Comparer<T>.(value, value2);
}
}
class Program
{
static void Main(string[] args)
{
GenStack<string> myStack = new GenStack<string>(10);
myStack.Add("BCCN");
myStack.Add("CSDN");
Console.WriteLine(myStack.ItemCount);
Console.WriteLine(myStack[0]);
int rel = ("BCCN", "CSDN");
Console.WriteLine(rel.ToString());
System.Threading.Thread.Sleep(10000);
}
}
}