标签:
1.测试
(1) 三种代码
伪代码、产品代码、测试代码
(2) TDD(Test Driven Devlopment, 测试驱动开发)
a.先写测试代码,然后再写产品代码的开发方法叫“测试驱动开发”(TDD)。TDD的一般步骤如下:
-明确当前要完成的功能,记录成一个测试列表
-快速完成编写针对此功能的测试用例
-测试代码编译不通过
-编写产品代码
-测试通过
-对代码进行重构,并保证测试通过
-循环完成所有功能的开发
b.基于TDD,我们不会出现过度设计的情况,需求通过测试用例表达出来了,我们的产品代码只要让测试通过就可以了。 Java中有单元测试工具JUnit来辅助进行TDD,红叉说明代码存在语法错误,原因很简单,MyUtil类还不存在,类中的percentage2fivegrade方法也不存在,我们在TDDDemo的src目录中新建一个MyUtil的类,并实现percentage2fivegrade方法
c.测试结果出现了一个绿条(green bar),说明测试通过了。
2. 三要素:封装、继承、多态
3.五个原则(solid)
S.O.L.I.D原则:
SRP(Single Responsibility Principle,单一职责原则)
OCP(Open-Closed Principle,开放-封闭原则)
LSP(Liskov Substitusion Principle,Liskov替换原则)
ISP(Interface Segregation Principle,接口分离原则)
DIP(Dependency Inversion Principle,依赖倒置原则)
Complex类代码
public class ComplexNumber {
public static void main(String[] args) {
Complex x = new Complex(1.0 , -2.0);
Complex y = new Complex(3.0 , 4.0);
Complex.printSentence(x, y);
}
}
class Complex {
private double a,b;
Complex(double a,double b) {
this.a = a;
this.b = b;
}
static Complex Add(Complex x,Complex y) {
return new Complex(x.a+y.a, x.b+y.b);
}
static Complex Minus(Complex x,Complex y) {
return new Complex(x.a-y.a, x.b-y.b);
}
static Complex Multiple(Complex x,Complex y) {
return new Complex(x.a * y.a - x.b * y.b, x.a * y.b + x.b * y.a);
}
static Complex Divide(Complex x,Complex y) {
Complex conjugate = new Complex(y.a, -y.b);
double mo = (y.a)*(y.a) + (y.b)*(y.b);
Complex t = Multiple(x,conjugate);
return new Complex(t.a/mo , t.b/mo);
}
static String print(Complex x) {
if (x.b > 0) return x.a + "+" + x.b + "i"; else return x.a + "-" + (-x.b) + "i";
}
static void printSentence(Complex x, Complex y) {
System.out.println("("+ print(x) + ")+("+ print(y) + ") =" +print(Add(x, y)));
System.out.println("("+ print(x) + ")-("+ print(y) + ") =" +print(Minus(x, y)));
System.out.println("("+ print(x) + ")*("+ print(y) + ") =" +print(Multiple(x, y)));
System.out.println("("+ print(x) + ")/("+ print(y) + ") =" +print(Divide(x, y)));
}
}
测试Complex类代码的ComplexTest类代码
public class ComplexTest extends Complex{
public void printTest{
Complex c1=new Complex(2,9);
Complex c2=new Complex(4,6);
Complex c=new Complex();
c.Multiple(c1,c2);
c.Add(c1,c2);
c.Minus(c1,c2);
}
标签:
原文地址:http://www.cnblogs.com/Lexington/p/5402862.html