如题
解决方案 »
- 从数据库中读出图片出现索引超出了数组界限
- App.config问题?
- 请问 怎样给GridView每行增加一个序号?
- C#如何将数据库中学生表转换成XML?
- 如何设置可以实现自动下载web服务器上的一个exe文件至本地
- 大婶进,一个关于AppDomain.Load的问题
- 怎么把一个 string 转换成 一个 int
- 求教:用Visual Studio .NET 2003 C#可否使用VC6、EVC4编写的ActiveX和COM?
- 我听在.NET里边可以对存储过程的每一条语句调试,请问么做到?100谢谢!
- C#中string与int的转换?
- 帮忙设计一个信用卡通用付账系统
- 关于无线网卡群发短信方面的知识,顶者有分!!!!
二、在.net中反射很方便当然,由于实现与接口的分离,更改实现不影响接口,除了这个优点,在.net中感觉是多余的。
-----------------------------------------------------------------------------------
别忘记.NET中大部分语言是单根继承的。
void f(A a);
也可以这样做
void f(IA a);
显然第二种的好处是,它不依赖于A的具体实现,而且对函数f也屏蔽了A中的一些其它的东西,这样也有得于安全编程
如果有两个接口分别是"球员"和"教练"
当有一个类是"人"类,这个类实现了"球员"和"教练"这两个接口
那么这个类就要有这两个接口的行为和职责当在调用"俱乐部"一个方法"入会"时,这个方法要传入一个"球员",那么传入"人"类的一个实例就行了.
在这个方法里面的代码只知道传入的是一个"球员"的实例,它可以调用"球员"中申明的方法.但如果"人"类没有实现"球员"接口,就不能传给"入会"方法了.在设计时,可用接口把一个类给分成几个部分了,也可以用接口把类的依赖关系倒过来(倒置原则).
可以看一些设计类的书,看多了就会了解到一些接口的使用.
如果:<设计模式>,<敏捷软件开发>
我想说的是接口的作用.也许是我的例子没有举好吧.也许你有更好的例子.无论是什么东西用好了就是好的,如果烂用了也不能怪东西不好.
同样一把菜刀可以用来切菜也可以用来杀人.
OO是个好东西,但也会被人烂用.-----------------------------------------------------------------------
我觉得楼主不只要理解,多一些实践,多看一些高手写的代码也会有很大的帮助的.
我真正学OOD也只是一年多,以前只是学了OO的语言和知道OO中有些什么东西.
希望大家多多交流.我的理解是:
就像演戏一样,如果一个类实现某个接口,那么就像是这个类扮演了某一个角色.
如果"人"类有实现"球员"这个接口,那么它就可以扮演"球员"这个角色.当然这只是个例子.对了,更正一下,鸭嘴兽只是哺乳动物(当然这个不是讨论的主题啦,不好意思)
举个例子:某公司有一台特殊打印机,还可以使用一年,一年后可能换为另一种打印机,这两种打印机都特殊而贵.所以现在的程序希望换了打印机后也少量修改就可用.
方法:
1,定义一个打印机接口.
2,定义打印机类A,B,分别实现此接口.
3,定义一个工厂类,在类中可选择返回由A实现的接口,或者由B实现的接口.
4,在程序中使用打印机时,就可以使用工厂类来调用打印机,而不需要知道具体的是什么打印机.
如果打印机换了,只需要修改工厂类就行了.如果有一千个地方都调用过打印机,就不需要一个一个修改.修改一个地方就行了.接口充当一个隔离层的作用.在面向对象的设计中,接口的作用非常重要,不是没用.
//定义打印机接口
interface Iprint{
bool PrintData(string data);
}//定义打印机类A,实现接口
class PrintA:Iprint{
public virtual bool PrintData(string data){
//具体业务逻辑略
}
}定义打印机类B,实现接口
class PrintB:Iprint{
public virtual bool PrintData(string data){
//具体业务逻辑略
}
}//定义工厂类
class PrintFactory{
public Iprint CreatePrint(){
//返回一个由打机类A,或B实现的接口,比如
return new PrintA();
}
}//通过工厂类,调用打印机
private void button1_Click(object sender,EventArgs e){
PrintFactory myFactory=new PrintFactory();
Iprint myPrint=myFactory.CreatePrint();
myPrint.PrintData("这样做很方便啊");
}
偶的想法
如果说类是对象间的抽象共有特征的话,那接口就是类间的抽象共有特征了,声明了这个特征就可以表示数个类而不必知道它是那个类的对象了,而一个类可以有几个特征。
按照.net类库中接口的用法给出这个例子: 接口Iflyable,几个类如Plane,Bird,SuperMan继承了它,你就可以用以下方法调用它而不管它是那一个类
void Act(Iflyable obj)
{
obj.BeginFly();
}
这样这个方法的使用适应性就强了很多了,符合OO模块化的要求,扩展起来很容易。这种东西在OO的高级设计里面比比皆是,楼主学过了用过了就会领悟的了。可能是c#1.0太单薄了,所以接口的用途还不是很多,不如abstract class 好用,像在.net类库里接口都是扩展功能为主,可能要到c#3.0才会有改善。
以上是我的个人想法,如有不对欢迎指正。
我不懂,但觉得对楼主有帮助
看书 ->不懂,学C++->虚类->COM接口->C#接口->终于懂了....
{
QuickSort(ref items, 0, items.Length - 1);
} private static void QuickSort<T>(ref T[] items,int fromIndex,int toIndex)
where T:IComparable<T>
{
if (fromIndex >= toIndex)
return;
else
{
int p = Partition(ref items, fromIndex, fromIndex+1, toIndex);
QuickSort(ref items, fromIndex, p - 1);
QuickSort(ref items, p + 1, toIndex);
}
} private static void swap<T>(ref T[] items,int index1,int index2)
{
if (index1 != index2)
{
T m = items[index1];
items[index1] = items[index2];
items[index2] = m;
}
} //使用T[]的第一个元素作为划分元素,重新排列T[]。返回划分元素的最终位置。
private static int Partition<T>(ref T[] items, int partIndex,
int fromIndex, int toIndex) where T : IComparable<T>
{
if (fromIndex > toIndex)
{
swap(ref items, partIndex, toIndex);
return toIndex;
}
else if (items[fromIndex].CompareTo(items[partIndex]) > 0)
//判断是否应当放在右边区域
{
swap(ref items, fromIndex, toIndex);
return Partition(ref items, partIndex, fromIndex, toIndex - 1);
}
else
return Partition(ref items, partIndex, fromIndex + 1, toIndex);
} //当要查找数组中按顺序第m位置的元素时,并不需要对数组排序。
public static T SelectMinM<T>(T[] items, int m) where T : IComparable<T>
{
return SelectMinM(ref items, m-1, 0, items.Length - 1);
} private static T SelectMinM<T>(ref T[] items, int m,int fromIndex,
int toIndex) where T : IComparable<T>
{
int p = Partition(ref items, fromIndex, fromIndex + 1, toIndex);
if (p == m)
return items[p];
else if (p < m)
return SelectMinM(ref items, m, p + 1, toIndex);
else
return SelectMinM(ref items, m, fromIndex, p - 1);
}
}
{
QuickSort(ref items, 0, items.Length - 1);
}private static void QuickSort<T>(ref T[] items, int fromIndex, int toIndex) where T : IComparable<T>
{
int p;
while (fromIndex < toIndex)
{
if (fromIndex < toIndex)
swap(ref items, fromIndex, Rnd.Next(fromIndex + 1, toIndex)); //避免最坏排序,将初始划分元素随机调换
p = Partition(ref items, fromIndex, fromIndex + 1, toIndex);
if (p - fromIndex <= toIndex - p)
{
QuickSort(ref items, fromIndex, p - 1);
fromIndex = p + 1;
}
else
{
QuickSort(ref items, p + 1, toIndex);
toIndex = p - 1;
}
}
}private static void swap<T>(ref T[] items, int index1, int index2)
{
if (index1 != index2)
{
T m = items[index1];
items[index1] = items[index2];
items[index2] = m;
}
}//使用T[]的第一个元素作为划分元素,重新排列T[]。返回划分元素的最终位置。
private static int Partition<T>(ref T[] items, int partIndex, int fromIndex, int toIndex) where T : IComparable<T>
{
while (true)
if (fromIndex > toIndex)
{
swap(ref items, partIndex, toIndex);
return toIndex;
}
else if (items[fromIndex].CompareTo(items[partIndex]) > 0) //判断是否应当放在右边区域
{
swap(ref items, fromIndex, toIndex);
toIndex -= 1;
}
else
fromIndex += 1;
}//当要查找数组中按顺序第m位置的元素时,并不需要对数组排序。
public static T SelectMinM<T>(T[] items, int m) where T : IComparable<T>
{
return SelectMinM(ref items, m - 1, 0, items.Length - 1);
}static Random Rnd = new Random();private static T SelectMinM<T>(ref T[] items, int m, int fromIndex, int toIndex) where T : IComparable<T>
{
int p;
while (true)
{
if (fromIndex < toIndex)
swap(ref items, fromIndex, Rnd.Next(fromIndex + 1, toIndex)); //避免最坏排序,将初始划分元素随机调换
p = Partition(ref items, fromIndex, fromIndex + 1, toIndex);
if (p == m)
return items[p];
else if (p < m)
fromIndex = p + 1;
else
toIndex = p - 1;
}
}
但使用com组件的时候就不能没有接口
{
}