111,092
社区成员




public partial class Optimization : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Employee emp = Employee.GetEmployee(EmployeeType.ENGINEER);
label_Show.Text += emp.GetDescription();
label_Show.Text += emp.PayAmount().ToString();
}
}
public enum EmployeeType
{
ENGINEER,
SALESMAN,
MANAGER
}
public abstract class Employee
{
protected int basicSalary;
protected int commission;
public static Employee GetEmployee(EmployeeType empType)
{
switch (empType)
{
case EmployeeType.ENGINEER:
return new Enginessr();
case EmployeeType.SALESMAN:
return new SalesMan();
case EmployeeType.MANAGER:
return new Manager();
default:
throw new Exception("no such employee type!");
}
}
public abstract int PayAmount();
public abstract string GetDescription();
}
public class Enginessr : Employee
{
public override int PayAmount()
{
return basicSalary;
}
public override string GetDescription()
{
return "Coding, Debug, Optimization";
}
}
public class SalesMan : Employee
{
public override int PayAmount()
{
return basicSalary + commission;
}
public override string GetDescription()
{
return "Getting contranct";
}
}
public class Manager : Employee
{
public override int PayAmount()
{
return 2 * basicSalary;
}
public override string GetDescription()
{
return "Analysis, Scheduling, Reporting";
}
}
// action声明
typedef void (*Action) (void);
// action 1, case 1时执行
void do_1()
{
cout << "action 1" << endl;
}
// action 2, case 2时执行
void do_2()
{
cout << "action 2" << endl;
}
// action选择器
void do_action(Action table[], int i)
{
table[i-1]();
}
int main()
{
// action表
Action table[2];
// 各case值注册到action表
table[0] = do_1;
table[1] = do_2;
// 根据值执行相应的action
do_action(table, 1);
return 0;
}