微科社区,轻松开发从此开始! 请登陆 免费注册

微科社区

当前位置:首页 > Java平台 > J2EE >

AOP面向切面编程(2)

时间:2017-01-12 04:01  浏览:努力统计中...
ArithmeticCalculator implements Calculator { @Override public double add( double num1, double num2) { System.out.println( the method [add()] + begin with args ( +num1+ , +num2+ ) ); double result = nu
ArithmeticCalculator implements Calculator
{
@Override
public double add(double num1, double num2)
{
System.out.println("the method [add()]"+"begin with args ("+num1+","+num2+")");
double result = num1 + num2;
System.out.println("the method [add()]"+"end with result ("+result+")");

return result;
}

@Override
public double sub(double num1, double num2)
{
System.out.println("the method [sub()]"+"begin with args ("+num1+","+num2+")");
double result = num1 - num2;
System.out.println("the method [sub()]"+"end with result ("+result+")");

return result;
}

/*示意代码 暂时不考虑除数0的情况*/
@Override
public double div(double num1, double num2)
{
System.out.println("the method [div()]"+"begin with args ("+num1+","+num2+")");
double result = num1 / num2;
System.out.println("the method [div()]"+"end with result ("+result+")");

return result;
}

@Override
public double mul(double num1, double num2)
{
System.out.println("the method [mul()]"+"begin with args ("+num1+","+num2+")");
double result = num1 * num2;
System.out.println("the method [mul()]"+"end with result ("+result+")");

return result;
}
}

若ArithmeticCalculator规定只能计算正数时,又需要添加参数验证方法:

/*计算器接口的实现类,添加记录日志功能*/
public class ArithmeticCalculator implements Calculator
{
@Override
public double add(double num1, double num2) throws Exception
{
this.argsValidatior(num1);
this.argsValidatior(num2);

/*同上*/
}

@Override
public double sub(double num1, double num2) throws Exception
{
this.argsValidatior(num1);
this.argsValidatior(num2);

/*同上*/
}

/*示意代码 暂时不考虑除数0的情况*/
@Override
public double div(double num1, double num2) throws Exception
{
this.argsValidatior(num1);
顶一下
(1)
100%
踩一下
(0)
0%
------分隔线------
栏目列表
推荐内容