using System;
using System.Collections;
public interface IA
{
void Add();
}
public class A:IA
{
public void Add()
{
Console.WriteLine("A:IA out put");
}
}
public class B:IA
{
public void Add()
{
Console.WriteLine("B:IA out put");
}
}
public interface IB
{
void Work();
}
public class C:A,IB
{
new public void Add()
{
Console.WriteLine("C:A,IB out put");
}
public void Work()
{
Console.WriteLine("C:work");
}
}
public class D:System.Windows.Forms.Form,IA
{
public void Add()
{
Console.WriteLine("D out put");
}
}
public class MyClass
{
public static void Main()
{
D d = new D();
IA a = (IA)d;
a.Add();
d.Add();//ok
//d.Show();
A a = new A();
a.Add();
IA ia = (IA)a;
ia.Add();
B b = (B)ia;// compiler error
b.Add();// error
C c = new C();//ok
c.Add();
IA ic = (IA)c;
ic.Add();
IB ib = (IB)c;
ib.Work();
A ca = (A)c;
ca.Add();
RL();
}
#region Helper methods
private static void WL(object text, params object[] args)
{
Console.WriteLine(text.ToString(), args);
}
private static void RL()
{
Console.ReadLine();
}
private static void Break()
{
System.Diagnostics.Debugger.Break();
}
#endregion
}
说明可以将*类直接强制转换为父类 并调用父类的接口;
we can cast the child class to parent class, and call the parent class's
methods.
但是如果强制转换父类为*类的话, 使能够转换,也会发生问题(这个在C ++*大家都明白的道理);
it's dangerous to cast a child class to parent class.
也可以把*类强制转化为父类的接口 ,并调用接口类*声明的接口;
we can cast a child class to parent interface, and can invoke the interface.
如果是通过Asembly提供的反射机制生成 类在进行强制转换的时候,在C#2* 编译错误,C#1*还没有测试,有兴 的也可以看看;
it throw compiler error information in C# 2.0, when i cast a class which
created by assembly reflect.
接下来的问题就是*究一下plugin技术 看看人家怎么实现的了:)
Hi,
I want to implement a common Form with special interface, such as
MovePoint(double,double).
I create a interface first:
namespace ABC.Test
{
public Interface IMyWindowInterface
{
void MovePoint(double,double);
}
}
then create a Form inherit form MyWindow:
namespace ABC.Test.A
{
public AWindow:Form,ABC.Test.IMyWindow
{
public void MovePoint(double x,double y)
{
//do work
}
}
public BWindow:AWindow,ABC.Test.IMyWindow
{
public void MovePoint(double x,double y)
{
//do work
}
}
public void Test()
{
BWindow oB = new BWindow();
AWindow oA = (AWindow)oB; //casting
exception
IMyWindow oI = (IMyWindow)oB; //casting
exception
AWindow oA = oB as AWindow; // 0k but it's
null
IMyWindow oI = oB as IMyWindow;
//why oA == null and oI == null?????
if(oA != null)
{
oA.MovePoint(1.0d,3.0d);
}
if(oI != null)
{
oI.MovePoint(1.0d,2.3d);
}
}
}
thanks!
harvie
2006-4-29