一直认为抽象类不能实例化,所以就没有想过用直接调用方法,而是实例化它的子类,用子类调用方法,
没想到啊,在抽象类中定义static方法不就可以了吗,static方法属于类,不需要实例化。
using System;
using System.Reflection;
namespace AbstractFactory
{
/// <summary>
/// Factory类
/// </summary>
public abstract class AbstractFactory
{
public static AbstractFactory GetInstance()
{
string factoryName = Constant.STR_FACTORYNAME.ToString();
AbstractFactory instance;
if(factoryName != "")
//AbstractFactory表示程序集的名称 ;factoryName表示要加载的类
instance = (AbstractFactory)Assembly.Load("AbstractFactory").CreateInstance(factoryName);
else
instance = null;
return instance;
}
public abstract Tax CreateTax();
public abstract Bonus CreateBonus();
}
}
using System;
namespace AbstractFactory
{
/// <summary>
/// 客户端程序调用
/// </summary>
public class Calculator
{
public static void Main(string[] args)
{
//GetInstance不是static方法
//AbstractFactory abstractFactory = new ChineseFactory().GetInstance();
//double bonusValue = abstractFactory.CreateBonus().Calculate();
//double taxValue = abstractFactory.CreateTax().Calculate();
//GetInstance是static方法
double bonusValue = AbstractFactory.GetInstance().CreateBonus().Calculate();
double taxValue = AbstractFactory.GetInstance().CreateTax().Calculate();
double salary = 4000 + bonusValue - taxValue;
Console.WriteLine("Salary is:" + salary);
Console.ReadLine();
}
}
}
using System;
namespace AbstractFactory
{
/// <summary>
/// ChineseFactory类
/// </summary>
public class ChineseFactory:AbstractFactory
{
public override Tax CreateTax()
{
return new ChineseTax();
}
public override Bonus CreateBonus()
{
return new ChineseBonus();
}
}
}
using System;
namespace AbstractFactory
{
/// <summary>
/// AmericanFactory类
/// </summary>
public class AmericanFactory:AbstractFactory
{
public override Tax CreateTax()
{
return new AmericanTax();
}
public override Bonus CreateBonus()
{
return new AmericanBonus();
}
}
}
using System;
namespace AbstractFactory
{
/// <summary>
/// 计算中国个人奖金
/// </summary>
public class ChineseBonus:Bonus
{
public override double Calculate()
{
return Constant.BASE_SALARY * 0.1;
}
}
}
using System;
namespace AbstractFactory
{
/// <summary>
/// 计算美国奖金
/// </summary>
public class AmericanBonus:Bonus
{
public override double Calculate()
{
return Constant.BASE_SALARY * 0.1;
}
}
}
using System;
namespace AbstractFactory
{
/// <summary>
/// 计算美国个人所得税
/// </summary>
public class AmericanTax:Tax
{
public override double Calculate()
{
return (Constant.BASE_SALARY + (Constant.BASE_SALARY * 0.1)) * 0.4;
}
}
}
using System;
namespace AbstractFactory
{
/// <summary>
/// 计算中国个人所得税
/// </summary>
public class ChineseTax:Tax
{
public override double Calculate()
{
return (Constant.BASE_SALARY + (Constant.BASE_SALARY * 0.1)) * 0.4;
}
}
}
using System;
using System.Configuration;
namespace AbstractFactory
{
/// <summary>
/// 公用的常量
/// </summary>
public class Constant
{
public static double BASE_SALARY = 4000;
public static string STR_FACTORYNAME = ConfigurationSettings.AppSettings["factoryName"];
}
}
using System;
namespace AbstractFactory
{
/// <summary>
/// 个人所得税抽象类
/// </summary>
public abstract class Tax
{
public abstract double Calculate();
}
}
using System;
namespace AbstractFactory
{
/// <summary>
/// 奖金抽象类
/// </summary>
public abstract class Bonus
{
public abstract double Calculate();
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="factoryName" value="AbstractFactory.ChineseFactory"></add>
</appSettings>
</configuration>