IFactory factory= new UndergraduateFactory()是什么意思?IFactory是接口啊??
C#, IFactory factory= new UndergraduateFactory();这句是什么意思?IFactory是接口啊??这里的IFactory是接口啊,不会是声明IFactory的对象,对象名为factory吧??在这段代码里IFactory是接口来的.具体是怎样理解这句话的??那factory这个对象是接口吗??声明为一个类型为IFactory的对象,不知道怎样理解??那factory是接口还是对象,是对象的话属于哪个类??
代码如下:
class LeiFeng
{
public void Sweep()
{
Console.WriteLine("扫地");
}
public void Wash()
{
Console.WriteLine("洗衣");
}
public void BuyRice()
{
Console.WriteLine("买米");
}
}
interface IFactory
{
LeiFeng CreateLeiFeng();
}
class UndergraduateFactory:IFactory
{
public LeiFeng CreateLeiFeng()
{
return new Undergraduate();
}
客户端代码
IFactory factory=new UndergraduateFactory();
LeiFeng student=factory.CreateLeiFeng();
student.BuyRice();
student.Sweep();
student.Wash();