标题:IFactory factory= new UndergraduateFactory()是什么意思?IFactory是接口啊 ...
只看楼主
a2220046
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-10-18
 问题点数:0 回复次数:2 
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();
搜索更多相关主题的帖子: factory IFactory 接口 new 
2008-11-02 09:28
a2220046
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-10-18
得分:0 
没有高手解释一下吗??
2008-11-05 01:12
mkxzy
Rank: 2
等 级:论坛游民
帖 子:39
专家分:40
注 册:2007-3-26
得分:0 
准确的写应该是IFactory factory=new UndergraduateFactory() as IFactory;
因为UndergraduateFactory类实现了IFactory接口,所以它是IFactory型的,可以通过这个接口访问它
2008-11-05 09:17



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-241951-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012443 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved