public interface IMyInterace
{
void DoSomething();
}
class Program:IMyInterace
{
public void DoSomething()
{
Console.WriteLine("111");
}
static void Main(string[] args)
{
Program t1 = new Program();
t1.DoSomething();
}
}
接口是那么用的吗?如果是的话,与以下代码有何区别: class Program
{
public void DoSomething()
{
Console.WriteLine("111");
}
static void Main(string[] args)
{
Program t1 = new Program();
t1.DoSomething();
}
}
{
void DoSomething();
}
class Program:IMyInterace
{
public void DoSomething()
{
Console.WriteLine("111");
}
static void Main(string[] args)
{
Program t1 = new Program();
t1.DoSomething();
}
}
接口是那么用的吗?如果是的话,与以下代码有何区别: class Program
{
public void DoSomething()
{
Console.WriteLine("111");
}
static void Main(string[] args)
{
Program t1 = new Program();
t1.DoSomething();
}
}
{
void Open();
void Close();
}public abstract class Door
{
void Open(){};
void Close(){};
}就目前来说, 你用Interface还是用abstract class基本上没什么区别, 两者都用来描述Door的行为, 只不过用Interface是在强调Door额外的有Open和Close这两个功能, 而用abstract class则是在强调Door自带有Open和Close的能力走远一点, 假如又有一天, 你要写一个AlarmDoor的类, 这个时候...public interface AlarmDoor
{
void open();
void close();
void alarm();
}public abstract class AlarmDoor
{
void open(){};
void close(){};
void alarm(){};
}现在, 你应该想清楚你究竟是想设计一个报警器还是想设计一扇门, 如果选择Interface, 那你是在想设计一个报警器, 如果选择abstract class, 那你是在想设计一扇门, 说到现在, 是否有点眉目了?C#里面不允许多个父类, 但却允许多个接口, 接口无非是让某个类拥有某种行为, 而抽象类却是在让某个类拥有某种本质, 因此, 我们设计的类, 应该用一个抽象类把它的所有本质都抽象出来, 而它的一些行为(或者说是契约)则通过Interface来实现, 这也能说明为什么C#会不允许多层父类却允许多层接口
最上面的应该改成
static void Main(string[] args)
{
IMyInterace t1 = new Program();
t1.DoSomething();
}
2 为何需要接口? 接口定义了一种规范,这让你的整个程序框架可读性增强.比如说吧,让你写一个汽车的类和自行车的类,如果你不继承,你该如何写? 用接口的话,可以定义它们共同的属性(外观颜色),方法(Run),不同的部分可以在继承类中扩展实现.简而言之,是OOP的思想;
3 C#不支持多继承,但是可以继承自多个接口其他,等楼下继续.
最上面的应该改成
static void Main(string[] args)
{
IMyInterace t1 = new Program();
t1.DoSomething();
}--------------------------------------有所领悟,但不明白的是:IMyInterace t1 = new Program();
Program t1 = new Program();这两个又有什么区别呢?不是都能调用
t1.DoSomething();的这个方法么
1.接口是为需编写的类提供一个蓝图。
2.接口只提供定义。
接口类似抽象方法,且为public
IMyInterace t1 就是说t1提供IMyInterace接口所定义的功能。
至于t1 = new Program();就是说这个接口是由Program类来实现的。比如一个函数定义
public void DoSomeFunction(IMyInterace t1)
{
t1.func1();
t1.func2();
...
}
这个函数不需要知道具体是哪个类实现了接口定义的内容。
{
void DoSomething();
}
class Program1:IMyInterace
{
public void DoSomething()
{
Console.WriteLine("111");
}
}
class Program2:IMyInterace
{
public void DoSomething()
{
Console.WriteLine("222");
}
}
class Program3:IMyInterace
{
public void DoSomething()
{
Console.WriteLine("333");
}
}class Program
{
static void Main(string[] args)
{
List<IMyInterace> list=new List<IMyInterace>();
list.Add(new Program1());
list.Add(new Program2());
list.Add(new Program3());
foreach(IMyInterace i in list)
{
i.DoSomething();
}
}
}你也可以用abstract class代替Interface,不过这两者是有区别,但与此问题无关,就不说了.
这种问题看书就能知道了.何必上来问? 多看下与设计模式相关的书吧.
Print(IEnumable i)
{
foreach(object o in i)
Console.WriteLine(o);
}那任何实现了IEnumable接口的类型都可以通过Print输出,ArrayList,数组等等,虽然他们具体实现不同。
如果你只有一个类在程序的同一个位置DoSomething()的话,你后面一种写法就OK了,用不着Interface.
但是,如果你的t1.DoSomething();可能会被两个(或者多个)不同的类调用,而且可能会在运行时才知道调用哪个类的DoSomething(),用Interface就比较好了。
像GalfordLiu(喜欢汪峰的歌)写的代码就很能说明这个问题。
什么是接口?
(1)接口对于实现者(实现类)来说是定义了一种规范,比如,如果你是Windows开发人员,为了能够查询每一个硬件的描述信息,你希望所有的硬件厂商在其驱动中实现IDescribable接口(由你提供),这个接口定义了Description属性,那么当硬件厂商实现了这个接口时,你就很容易去取得每一个硬件设备的描述信息。
(2)在C#中,不支持多重继承,因此对于C#来说,接口是协助实现多重继承的工具。
(3)抽象上说,接口就是定义了某个对象的在某个特定方面的外在表现,通过实现多个接口,同一个对象对于不同的使用者反应出多个不同的面和交互方式。
(4)为了实现版本的要求,接口应该具有不变性,也就是说,接口的定义者,在实现者发布他们的对象后,不能随意修改接口定义,否则接口就会失去其意义,这就是为什么,你会经常看到IAbc,IAbc2:IAbc,...。
接口是那么用的吗? 可以肯定的告诉用法是正确的。
区别是什么? 如果就LZ的程序实现功能来说,他是没有区别的
如果真是进行区别,也就是楼上高歌所说的抽象类
(abstract class)和接口(interface)之间的区别。应该说最大的区别就是在C#中,不支持多重继承,因此对于C#来说,接口是协助实现多重继承的工具。
让你明白什么是接口
我只是把你的代码改了下.. 实际上接口不会这么用,也永远不要那么用...!!!
一开始我也这么怀疑过,为什么要留个接口这种不包含实际功能的东西.. public interface IMyInterace
{
void DoSomething();
}
class Program2:IMyInterace
{
public void DoSomething()
{
Console.WriteLine("111");
}
}
class Program:IMyInterace
{
public void DoSomething()
{
Console.WriteLine("111");
}
static void test(IMyInterace arg)
{
arg.DoSomething();
}static void Main(string[] args)
{
Program t1 = new Program();
test(t1);
Program2 t2 = new Program2();
test(t2);
}
} 很显然接口会提高程序的灵活性,虽然不是必须用接口不可! 另外如数据操作, 写在一个接口里,那么可以不管他是以什么方式实现,
操作只操作接口, 实现接可的类可以用sql server,access,mysql进行数据操作.. 还有统一约束的问题 , 就比如数组, 返正都实现了IList 你就可以操作IList里的方法,
管他是[]还是ArrayList 什么的 . 传参,只传接口也更灵活,因为你可以把很多类型传进去
只要实现接口
public interface Ishape
{
void draw();
}class Round:Ishape
{
public void draw()
{/*..画一个圆形....*/}
public void otherThing()
{/*.......*/}
}class Square:Ishape
{
public void draw()
{/*.画一个方形...*/}
public void otherThing()
{/*.......*/}
}class methed
{
/*然而 display 传递的是一具体类的话,效果是相当差的,灵活性也受到了限制*/
public static void display(Ishape shape)
{
shape.draw();
}
}
class MainClass
{
main()
{
Square mySquare=new Square();
Round myRound=new Round();
methed.display(mySquare);
methed.display(myRound);
}
}
打个比方:你要计算所有图形面积,比如圆、矩形、3角型等等。那么他们就有一个共同的方法GetArea().我们就可以把这个公共的方法抽象出一个接口 IArea
interface IArea
{
double GetArea();
}那么我们在编写各个图形类的时候就必须实现这个IArea接口
即:
class Circle:IArea
{
double GetArea()
{
....
}
}class Rectangle:IArea
{
double GetArea()
{
....
}
}那么我们在实例对象时可以这么定义
IArea Obj;
Obj = new Circle();
Obj.GerArea();
从接口我们可以实现多态。
class c1
{
m1();
m2();
m3();
}
class c2
{
m2();
m3();
m4();
}
这时就可以把m2,m3作为接口。
仅是我的理解。
class Program
{
public void DoSomething()
{
Console.WriteLine( "111 ");
}
static void Main(string[] args)
{
Program t1 = new Program();
t1.DoSomething();
}
}和
class Program
{
static void Main(string[] args)
{
Console.WriteLine( "111 ");
}
}有什么区别??
只是实现的方法谁更流行谁更能被人为够合理或者更COOL。
至少现在面向对象是被认为合理的。所以,去读thinking in java吧。
没有哪个水池会要求一定要装某具体型号的水龙头(class)吧?这样系统实际构建你想想多不灵活?
比如人来说:同样是人,都有相同的五官,四肢,却又不同的能力。
相同的可用虚类,
不同的可用接口(因为人与人之间不同之处大于相同之处,这也就是多继承的好好)