// Use this for initialization void Start() { baseClass baseClassInstance = new baseClass(); derivedClass derivedClassInstance = new derivedClass(); //假若<A>is<B>中,<A>是<B>或<B>的派生类的实例,则返回true print(derivedClassInstance is derivedClass); print(derivedClassInstance is baseClass); classWithInterface classWithInterfaceInstance = new classWithInterface(); myInterface myInterfaceInstance = classWithInterfaceInstance; //接口变量与接口进行比较 print(myInterfaceInstance is myInterface); //实例与接口进行比较 print(classWithInterfaceInstance is myInterface); }
//method called myMethod that passes in an object public static void myMethod(object myObject) { //interface ref variable myInterface myNewIntface = (myInterface)myObject; //print the cool Method for the obj if (myObject.GetType() == typeof(my1stClass)) { //use class 1 method myNewIntface.Cool(); } else if (myObject.GetType() == typeof(my2ndClass)) { //use class 2 method myNewIntface.Cool(); } else { Console.WriteLine("Not myClass1 or myClass2 objecct"); } }
public static int Main_old() { myInterface[] arr = new myInterface[10]; return 0; }