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.length());System.out.println(buffer.indexOf("tring"));System.out.println("buffer = " + buffer.toString());
將方法進行重寫,添加返回值,以便于測試
public class StringBufferDemo{StringBuffer buffer = new StringBuffer();public StringBufferDemo(StringBuffer buffer){this.buffer = buffer;}public Character charAt(int i){return buffer.charAt(i);}public int capacity(){return buffer.capacity();}public int length(){return buffer.length();}public int indexOf(String buf) {return buffer.indexOf(buf);}
}
// Server Classes
abstract class Data {abstract public void DisplayValue();
}
class Integer extends Data {int value;Integer() {value=100;}public void DisplayValue(){System.out.println (value);}
}
class Byte extends Data {byte value;Byte(){value=127;}public void DisplayValue(){System.out.println(value);}
}
abstract class Factory {abstract public Data CreateDataObject();
}
class IntFactory extends Factory {public Data CreateDataObject(){return new Integer();}
}
class ByteFactory extends Factory {public Data CreateDataObject(){return new Byte();}
}
class Document {Data pd;Document(Factory pf){pd = pf.CreateDataObject();}public void DisplayData(){pd.DisplayValue();}
}
public class MyDoc {static Document d;static Document e;public static void main(String[] args) {e=new Document(new ByteFactory());e.DisplayData();}
}
運行結果:
(七)以TDD的方式開發一個復數類Complex
任務:以TDD的方式開發一個復數類Complex,要求如下:
// 定義屬性并生成getter,setter
double RealPart;
double ImagePart;
// 定義構造函數
public Complex()
public Complex(double R,double I)//Override Object
public boolean equals(Object obj)
public String toString()// 定義公有方法:加減乘除
Complex ComplexAdd(Complex a)
Complex ComplexSub(Complex a)
Complex ComplexMulti(Complex a)
Complex ComplexDiv(Complex a)
本道練習題基本考察TDD編碼的節奏。因為本題中實部虛部很容易混淆,導致出錯
因此根據測試代碼來修改產品代碼的優越性得以體現。
產品代碼:
public class Complex{private double a;private double b;public Complex(double a, double b) {this.a = a;this.b = b;}public static double getRealPart(double a) {return a;}public static double getImagePart(double b) {return b;}public Complex ComplexAdd(Complex c) {return new Complex(a + c.a, b + c.b);}public Complex ComplexSub(Complex c) {return new Complex(a - c.a, b - c.b);}public Complex ComplexMulti(Complex c) {return new Complex(a * c.a - b * c.b, a * c.b + b * c.a);}public Complex ComplexDiv(Complex c) {return new Complex((a * c.b + b * c.a)/(c.b * c.b + c.a * c.a), (b * c.b + a * c.a)/(c.b * c.b + c.a * c.a));}public String toString() {String s = " ";if (b > 0)s = a + "+" + b + "i";if (b == 0)s = a + "";if (b < 0)s = a + " " + b + "i";return s;}
}
public abstract class Animal {private String color;public String getColor() {return color;}public void setColor(String color) {this.color = color;}public abstract String shout();
}
public class Dog extends Animal{public String shout(){return "汪汪";}public String toString(){return "The Dog's color is " + this.getColor() +", and it shouts "+ this.shout() + "!";}
}
public class Cat extends Animal{public String shout(){return "喵喵";}public String toString(){return "The Cat's color is " + this.getColor() +", and it shouts "+ this.shout() + "!";}
}