Thinking in Java 第四版完整版 第八章练习题,记录一下(jdk1.8.0)
1.
/**
* 练习1:创建一个Cycle类,它具有子类Unicycle、Bicycle和
* Tricycle。演示每一个类型的示例都可以经由ride()方法向上
* 转型为Cycle。
* @author admin11
* @date 2018年3月28日
*/
class Cycle {}
class Unicycle extends Cycle {}
class Bicycle extends Cycle {}
class Tricycle extends Cycle {}
public class Exercise801 {
public static void ride(Cycle cycle) {}
public static void main(String[] args) {
ride(new Unicycle());
ride(new Bicycle());
ride(new Tricycle());
}
}
2.
/**
* 练习2:在几何图形的示例中添加@Override注解。
* @author admin11
* @date 2018年3月28日
*/
public class Shape {
public void draw() {}
public void erase() {}
}
public class Circle extends Shape {
@Override
public void draw() {
System.out.println("Circle.draw()");
}
@Override
public void erase() {
System.out.println("Circle.erase()");
}
}
public class Square extends Shape{
@Override
public void draw() {
System.out.println("Square.draw()");
}
@Override
public void erase() {
System.out.println("Square.erase()");
}
}
public class Triangle extends Shape{
@Override
public void draw() {
System.out.println("Triangle.draw()");
}
@Override
public void erase() {
System.out.println("Triangle.erase()");
}
}
import java.util.Random;
public class RandomShapeGenerator {
private Random rand = new Random(47);
public Shape next() {
switch (rand.nextInt(3)) {
default:
case 0:
return new Circle();
case 1:
return new Square();
case 2:
return new Triangle();
}
}
}
public class Shapes {
private static RandomShapeGenerator gen = new RandomShapeGenerator();
public static void main(String[] args) {
Shape[] s = new Shape[9];
for (int i = 0; i < s.length; i++) {
s[i] = gen.next();
}
for (Shape shape : s) {
shape.draw();
}
}
}

3.
/**
* 练习3:在基类Shapes.java中添加一个新方法,用于打印一条消息,
* 但导出类中不要覆盖这个方法。请解释发生了什么。现在,在其中一个导出
* 类中覆盖该方法,而在其他的导出类中不予覆盖,观察又有什么发生。最后,
* 在所有的导出类中覆盖这个方法。
* @author admin11
* @date 2018年3月28日
*/
public class Shape {
public void draw() {}
public void erase() {}
public void testPrint() {
System.out.println("shape.testPrint()");
}
}
public class Circle extends Shape {
@Override
public void draw() {
System.out.println("Circle.draw()");
}
@Override
public void erase() {
System.out.println("Circle.erase()");
}
@Override
public void testPrint() {
System.out.println("Circle.testPrint()");
}
}
public class Square extends Shape{
@Override
public void draw() {
System.out.println("Square.draw()");
}
@Override
public void erase() {
System.out.println("Square.erase()");
}
@Override
public void testPrint() {
System.out.println("Square.testPrint()");
}
}
public class Triangle extends Shape{
@Override
public void draw() {
System.out.println("Triangle.draw()");
}
@Override
public void erase() {
System.out.println("Triangle.erase()");
}
@Override
public void testPrint() {
System.out.println("Triangle.testPrint()");
}
}
import java.util.Random;
public class RandomShapeGenerator {
private Random rand = new Random(47);
public Shape next() {
switch (rand.nextInt(3)) {
default:
case 0:
return new Circle();
case 1:
return new Square();
case 2:
return new Triangle();
}
}
}
public class Shapes {
private static RandomShapeGenerator gen = new RandomShapeGenerator();
public static void main(String[] args) {
Shape[] s = new Shape[9];
for (int i = 0; i < s.length; i++) {
s[i] = gen.next();
}
for (Shape shape : s) {
shape.draw();
shape.testPrint();
}
}
}

4.
/**
* 练习4:向Shapes.java中添加一个新的Shape类型,并在main()
* 方法中验证:多态对新类型的作用是否与在旧类型中的一样。
* @author admin11
* @date 2018年3月28日
*/
class Shape {
public void draw() {}
public void erase() {}
public void testPrint() {
System.out.println("shape.testPrint()");
}
}
class Circle extends Shape {
@Override
public void draw() {
System.out.println("Circle.draw()");

1715

被折叠的 条评论
为什么被折叠?



