标签:buffer 键盘 throw cad message static not ade 编程模式
编程标准包含:具有说明性的名字、清晰的表达式、直截了当的控制流、可读的代码和注释,以及在追求这些内容时一致地使用某些规则和惯用法的重要性。
public class CodeStandard {
public static void main(String [] args){
StringBuffer buffer = new StringBuffer();
buffer.append(‘S‘);
buffer.append("tringBuffer");
System.out.println(buffer.charAt(1));
System.out.println(buffer.capacity());
System.out.println(buffer.indexOf("tring"));
System.out.println("buffer = " + buffer.toString());
if(buffer.capacity()<20)
buffer.append("1234567");
for(int i=0; i<buffer.length();i++)
System.out.println(buffer.charAt(i));
}
}
code
菜单规范代码:Code->Reformate Code
public class CodeStandard {
public static void main(String[] args) {
StringBuffer buffer = new StringBuffer();
buffer.append(‘S‘);
buffer.append("tringBuffer");
System.out.println(buffer.charAt(1));
System.out.println(buffer.capacity());
System.out.println(buffer.indexOf("tring"));
System.out.println("buffer = " + buffer.toString());
if (buffer.capacity() < 20)
buffer.append("1234567");
for (int i = 0; i < buffer.length(); i++)
System.out.println(buffer.charAt(i));
}
}
code
更多的功能
Override Methods
:重载基本类的方法;Implement Methods
:完成当前类 implements 的(或者抽象基本类的)接口的方法;Generate
:创建类里面任何字段的 getter 与 setter 方法;Surround With
:使用if-else
、try-catch
、do-while
等包装代码段使用Surround With
(Ctrl+Alt+T):使用if-else
包装代码
使用IDEA插件
结对编程是XP中的重要实践。在结对编程模式下,一对程序员肩并肩、平等地、互补地进行开发工作。他们并排坐在一台电脑前,面对同一个显示器,使用同一个键盘、同一个鼠标一起工作。他们一起分析,一起设计,一起写测试用例,一起编码,一起做单元测试,一起做集成测试,一起写文档等。
结对编程中有两个角色:
- 驾驶员(Driver)是控制键盘输入的人。
- 领航员(Navigator)起到领航、提醒的作用。
在码云上把自己的学习搭档加入自己的项目中,确认搭档的项目加入自己后,下载搭档实验二的Complex代码,加入不少于三个JUnit单元测试用例,测试成功后git add .; git commit -m "自己学号 添加内容";git push;
public class Complex {
private double RealPart;
private double ImagePart;
public double getterRealPart() {
return this.RealPart;
}
public double getterImagePart() {
return this.ImagePart;
}
public static double getterRealPart(double RealPart) {
return RealPart;
}
public static double getterImagePart(double ImagePart) {
return ImagePart;
}
public void setterRealPart(double RealPart) {
this.RealPart = RealPart;
}
public void setterImagePart(double ImagePart) {
this.ImagePart = ImagePart;
}
public Complex() {
this.RealPart = 1;
this.ImagePart = 2;
}
public Complex(double R, double I) {
this.RealPart = R;
this.ImagePart = I;
}
//Override Object
public boolean equals(Object obj) {
Complex complex = (Complex) obj;
if (this == obj) {
return true;
} else if (!(obj instanceof Complex)) {
return false;
} else if (getterRealPart() != complex.getterRealPart()) {
return false;
} else if (getterImagePart() != complex.getterImagePart()) {
return false;
} else
return true;
}
public String toString() {
if (getterRealPart() == 0)
return getterImagePart() + "i";
else if (getterImagePart() == 0)
return getterRealPart() + "";
else if (getterImagePart() < 0)
return getterRealPart() + "" + getterImagePart() + "i";
else
return getterRealPart() + "+" + getterImagePart() + "i";
}
// 定义公有方法:加减乘除
Complex ComplexAdd(Complex a) {
return new Complex(this.getterRealPart() + a.getterRealPart(), getterImagePart() + a.getterImagePart());
}
Complex ComplexSub(Complex a) {
return new Complex(this.getterRealPart() - a.getterRealPart(), getterImagePart() - a.getterImagePart());
}
Complex ComplexMulti(Complex a) {
return new Complex(this.getterRealPart() * a.getterRealPart() - a.getterImagePart() * this.getterImagePart(), a.getterImagePart() * this.getterRealPart() + a.getterRealPart() * this.getterImagePart());
}
Complex ComplexDiv(Complex a) {
Complex c = new Complex();
if (a.equals(c)) {
System.out.println("错误,分母不能为零!");
}
return new Complex(this.getterRealPart() / a.getterRealPart(), this.getterImagePart() / a.getterImagePart());
}
}
import junit.framework.TestCase;
import org.junit.Test;
public class ComplexTest extends TestCase {
Complex complex1 = new Complex(3, 4);
Complex complex2 = new Complex(1, -2);
Complex complex3 = new Complex(1, 1);
@Test
public void testgetterRealPart() throws Exception {
assertEquals(3.0, Complex.getterRealPart(3.0));
assertEquals(1.0, Complex.getterRealPart(1.0));
assertEquals(-2.0, Complex.getterRealPart(-2.0));
}
@Test
public void testgetterImagePart() throws Exception {
assertEquals(4.0, Complex.getterImagePart(4.0));
assertEquals(-2.0, Complex.getterImagePart(-2.0));
assertEquals(0.0, Complex.getterImagePart(0.0));
}
@Test
public void testAdd() throws Exception {
assertEquals("4.0+2.0i", complex1.ComplexAdd(complex2).toString());
assertEquals("4.0+5.0i", complex1.ComplexAdd(complex3).toString());
assertEquals("2.0-1.0i", complex2.ComplexAdd(complex3).toString());
}
@Test
public void testSub() throws Exception {
assertEquals("2.0+6.0i", complex1.ComplexSub(complex2).toString());
assertEquals("2.0+3.0i", complex1.ComplexSub(complex3).toString());
assertEquals("-3.0i", complex2.ComplexSub(complex3).toString());
}
@Test
public void testMulti() throws Exception {
assertEquals("11.0-2.0i", complex1.ComplexMulti(complex2).toString());
assertEquals("-1.0+7.0i", complex1.ComplexMulti(complex3).toString());
assertEquals("3.0-1.0i", complex2.ComplexMulti(complex3).toString());
}
@Test
public void testDiv() throws Exception {
assertEquals("3.0-2.0i", complex1.ComplexDiv(complex2).toString());
assertEquals("3.0+4.0i", complex1.ComplexDiv(complex3).toString());
assertEquals("1.0-2.0i", complex2.ComplexDiv(complex3).toString());
}
}
测试截图
上传到结对搭档的码云项目
重构(Refactor),就是在不改变软件外部行为的基础上,改变软件内部的结构,使其更加易于阅读、易于维护和易于变更 。
一个完整的重构流程包括:
搭档代码与问题
修改后代码
Bad Smell
伪代码
(1) 生成MessageDigest对象
(2) 传入需要计算的字符串,生成字符串数组
(3) 计算消息摘要
(4) 处理计算结果,将结果转为字符串
?
源代码
重构代码
?
编写测试代码并规范代码
测试代码
分工:
我完成伪代码与源代码,搭档进行重构与代码测试,最终将完善的产品代码上传至码云
步骤 | 耗时 | 百分比 |
---|---|---|
需求分析 | 12min | 10% |
设计 | 10min | 8% |
代码实现 | 48min | 40% |
测试 | 40min | 34% |
分析总结 | 10min | 8% |
标签:buffer 键盘 throw cad message static not ade 编程模式
原文地址:https://www.cnblogs.com/atbaoi/p/8995596.html