先附上Main()下的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
double numberA;
double numberB;
string oper;
//为什么要声明基类Operatcion??????
Operation a = null; Console.WriteLine("请输入第一个数字:");
numberA = double.Parse(Console.ReadLine());
Console.WriteLine("请输入操作运算符: + - * / ");
oper = Console.ReadLine();
Console.WriteLine("请输入第二个数字:");
numberB = double.Parse(Console.ReadLine());
//A被赋值工厂是为了实现多态
a = OperationFactory.CrateFactory(oper);
a.NumberA = numberA;
a.NumberB = numberB; Console.WriteLine(a.GetResult());
}
}
}
以上代码我不理解为什么要声明基类Operatcion附上创建工厂的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication2
{
class OperationFactory
{
/// <summary>
/// 变量OP用来接前台传来的运算符在下边Switch中进行判断从而使基类指向不同的派生类对象
/// 为什么要用Operatcion类类型???????
/// </summary>
/// <param name="op"></param>
/// <returns></returns> public static Operation CrateFactory(string op)
{
//声明基类是为了可以指向不同的派生类
Operation oper = null;
switch (op)
{
case "+":
oper = new OperationAdd();
break;
}
return oper;
}
}
}public static Operation CrateFactory(string op)
为什么在创建工厂的时候要用Operatcion类类型?其它的方法不可以吗?等待解答!~~谢谢了!~
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
double numberA;
double numberB;
string oper;
//为什么要声明基类Operatcion??????
Operation a = null; Console.WriteLine("请输入第一个数字:");
numberA = double.Parse(Console.ReadLine());
Console.WriteLine("请输入操作运算符: + - * / ");
oper = Console.ReadLine();
Console.WriteLine("请输入第二个数字:");
numberB = double.Parse(Console.ReadLine());
//A被赋值工厂是为了实现多态
a = OperationFactory.CrateFactory(oper);
a.NumberA = numberA;
a.NumberB = numberB; Console.WriteLine(a.GetResult());
}
}
}
以上代码我不理解为什么要声明基类Operatcion附上创建工厂的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication2
{
class OperationFactory
{
/// <summary>
/// 变量OP用来接前台传来的运算符在下边Switch中进行判断从而使基类指向不同的派生类对象
/// 为什么要用Operatcion类类型???????
/// </summary>
/// <param name="op"></param>
/// <returns></returns> public static Operation CrateFactory(string op)
{
//声明基类是为了可以指向不同的派生类
Operation oper = null;
switch (op)
{
case "+":
oper = new OperationAdd();
break;
}
return oper;
}
}
}public static Operation CrateFactory(string op)
为什么在创建工厂的时候要用Operatcion类类型?其它的方法不可以吗?等待解答!~~谢谢了!~
解决方案 »
- 求一段代码,实现100万个6位随机数里面出现次数最低的代码
- 求个读写ini文件配置信息的源码。
- 如何在WinForm里打开一个刘篮球窗口
- C# 响应控制台(Console)关闭事件时报错,高手请入
- 用户控件添加属性的问题
- C#控件问题
- DataGrid 分页 (Winform + Access数据库)有什么好办法???
- 循环里面需要有个变量怎么办? for (int i = 0; i < 10; i++) 和 for(int i = 0; i < 0; i++)
- 用代码添加 office2007的 选项卡 帮下忙吧
- ASP写的cookie在ASP.NET中如何修改它的值?急!!!
- 请教:如何保存Graphic对象已绘制的内容
- Devexpress.XtraGrid CardView设置垂直滚动条
谢谢Fangxinggood的回答,不过对我这样的初学者来说,不太直白!~我有些不太好理解!~~呵呵!~~
比如你要找个老婆,是女人("Operation")就行了,管她是春哥("+")还是曾哥("-")
(1)学习设计模式,找本好书,找个好例子。
(2)重思想方法,而不是编码技巧。为什么用到工厂,工厂的本质是什么是重点。不理解这些看这些为了模式而模式的例子没有意义。
(3)设计模式的实现随着时间的推移,已经没有用了。事实上现在的编程语言,几乎用不到设计模式。
比如说C#的委托,代替了策略模式。
装箱拆箱,代替了享元。
工厂模式、适配器模式被动态方法以及表达式API取代。
迭代器模式被自动迭代器取代。
模板模式几乎没用了。
纯静态类取代了单例。
事件取代了观察者模式……
所以设计模式只是一种思维方法,不要去钻那些没用的代码。
用户请求一个产品,该工厂创建不同的子类,然后作为父类返回。
person类public class person
{
public person(){}
public person(string name,int age)
{
this.Name = name;
this.Age = age;
}
public string Name{get;set;}
public int Age{get;set;}
}student类public class student : person
{
public student(){}
public student(string name,int age,string hobby)
:base(name,age)
{
this.Hobby = hobby;
}
public string hobby{get;set;} //student类特有属性
}teacher类public class teacher : person
{
public teacher(){}
public teacher(string name,int age,int yearsOfService)
:base(name,age)
{
this.YearsOfService = yearsOfService;
}
public int YearsOfService{get;set;} //teacher类特有属性
}
简单工厂(SimpleFactory)代码示例public class SimpleFactory
{
public static person CreateProduct(string type,string name,int age,
string hobby,int yearsOfService)
{
//定义一个person类对象
person per = null; switch(type) //看用户请求哪个子类对象,判断后,传入对应参数,后作为父类返回
{
case"student":
per = new student(name,age,hobby); //向上传递
break;
case"teacher":
per = new teacher(name,age,yearsOfService);
break;
}
return per;
}
}