类,接口! 一个类实现了多个接口中的方法,在实际应用中是用类来调用方法?,还是用接口来调用方法? 代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace 有关类的技术实验3 { public interface IMyInterface { void Method();
} public interface IMyInterface2 { void Method2();
} public class MyClass : IMyInterface, IMyInterface2 { public void Method() { Console.WriteLine("Greetings from IMyInterface "); }
public void Method2() { Console.WriteLine("Greetings from IMyInterface2"); } }
class Program {
static void Main(string[] args) { IMyInterface a = new MyClass(); a.Method(); IMyInterface2 b = new MyClass(); b.Method2(); MyClass c = new MyClass(); c.Method(); c.Method2(); Console.ReadKey(); } } }
实际应用中,是红色的使用的多,还是天蓝色的使用的多?,使用这两种方法技术上的称呼叫什么? 还有 IMyInterface a = new MyClass(); a.Method(); 这段代码,听大牛们说,new是创建了一个引用,把地址传给了a,我的问题是,类的地址传给了接口,这样的隐式转换没问题吗?我只看到过,继承类可以隐式地转换给基类... ...
Because classes are reference types, a variable of a class object holds a reference to the address of the object on the managed heap. If a second object of the same type is assigned to the first object, then both variables refer to the object at that address. This point is discussed in more detail later in this topic.