首页 > 代码库 > 简单工厂

简单工厂

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

/*

简单工厂

*/
namespace App_MYCS.HDL_SJMS.JDGC
{
//调用
public class my_JDGC_dy
{
public void my_mydycx()
{
class_Fl myclass = my_JDGC.GetObj("+");
myclass.A1 = 1;
myclass.A2 = 2;
int s=myclass.appfunction();
}
}

//对外只有工厂类
public class my_JDGC
{
//工厂类生成返回对象
public static class_Fl GetObj(string objstr)
{
try
{
class_Fl cfl = null;
switch (objstr)
{
case "+":
cfl = new class_zl_1();
break;
case "-":
cfl = new class_zl_2();
break;
case "*":
cfl = new class_zl3();
break;
default:
break;
}
return cfl;
}
catch (Exception)
{
return null;
}
}
}

// 父类
public class class_Fl
{

//抽取同类的几个类的公共属性
//和公共方法(同类方法)


private int a1;

public int A1
{
get { return a1; }
set { a1 = value; }
}
private int a2;

public int A2
{
get { return a2; }
set { a2 = value; }
}

public virtual int appfunction()
{
return A1 + A2;
}

}

//各个子类
public class class_zl_1 : class_Fl
{
public override int appfunction()
{
return A1 + A2;
}
}

public class class_zl_2 : class_Fl
{
public override int appfunction()
{
return A1-A2;
}

}

public class class_zl3 : class_Fl
{
public override int appfunction()
{
return A1*A2;
}
}
}

简单工厂