标签:method new interface 静态 inter key 调用 code ogr
想法如下:
当前类A中使用另一个类B中的静态方法。这两个都不是静态类,可实例化。
没啥实质性的用处,做个思维拓展
class Program { static void Main(string[] args) { Console.ReadKey(); } } public class DefineClassA { public static void ClassA() { Console.WriteLine("DefineClassA-ClassA"); } } public class DefineClassB { public static void ClassB() { Console.WriteLine("DefineClassB-ClassB"); } }
如上,如果想在DefineClassA中调用ClassA那么直接 DefineClassA.ClassA 就好了,那如果想用 DefineClassA 来使用ClassB呢?
第一种:
class Program { static void Main(string[] args) { DefineClassA.ClassA(); DefineClassA.ClassB(); Console.ReadKey(); } } public class DefineClassA { public static void ClassA() { Console.WriteLine("DefineClassA-ClassA"); } public static void ClassB() { DefineClassB.ClassB(); } } public class DefineClassB { public static void ClassB() { Console.WriteLine("DefineClassB-ClassB"); } }
第二种:
class Program { static void Main(string[] args) { DefineClassA.ClassA(); DefineClassA.ClassB(); Console.ReadKey(); } } public class DefineClassA: DefineClassB { public static void ClassA() { Console.WriteLine("DefineClassA-ClassA"); } } public class DefineClassB { public static void ClassB() { Console.WriteLine("DefineClassB-ClassB"); } }
第三种:
class Program { static void Main(string[] args) { DefineClassA.ClassA(); var type = DefineClassA._DefineClassB.GetType(); var GetMethod = DefineClassA._DefineClassB.GetType().GetMethod("ClassB"); GetMethod.Invoke(type, null); Console.ReadKey(); } } public class DefineClassA { public static DefineClassB _DefineClassB = new DefineClassB(); public static void ClassA() { Console.WriteLine("DefineClassA-ClassA"); } } public class DefineClassB { public static void ClassB() { Console.WriteLine("DefineClassB-ClassB"); } }
第四种:
class Program { static void Main(string[] args) { BaseClassA baseClassA = new BaseClassA(); BaseClassB baseClassB = new BaseClassB(); baseClassA.Print(baseClassB); Console.ReadKey(); } } public interface interfaceBase { void PrintBase(); } public class BaseClassA : interfaceBase { public BaseClassA() { } public void PrintBase() { Console.WriteLine("BaseClassA"); } public void Print(interfaceBase interfaceBase) { interfaceBase.PrintBase(); } } public class BaseClassB : interfaceBase { public BaseClassB() { } public void PrintBase() { Console.WriteLine("BaseClassB"); SayStatic(); } public static void SayStatic() { Console.WriteLine("BaseClassB-SayStatic"); } }
标签:method new interface 静态 inter key 调用 code ogr
原文地址:https://www.cnblogs.com/myloveblogs/p/12885921.html