shapes4(1).java

来自「JAVA编程思想第四版英文原版习题答案. pdf原版的」· Java 代码 · 共 48 行

JAVA
48
字号
// typeinfo/Shapes4.java
// TIJ4 Chapter Typeinfo, Exercise 4, page 561
/* Modify the previous exercise so that it uses instancof to check the
* type before performing the downcast.
*/
import java.util.*;

abstract class Shape {
	void draw() { System.out.println(this + ".draw()"); }
	abstract public String toString();
}

class Circle extends Shape {
	public String toString() { return "Circle"; }
}

class Square extends Shape {
	public String toString() { return "Square"; }
}

class Triangle extends Shape {
	public String toString() { return "Triangle"; }
}

class Rhomboid extends Shape {
	public String toString() { return "Rhomboid"; }
}

public class Shapes4 {
	public static void main(String[] args) {
		// upcasting to Shape:
		List<Shape> shapeList = Arrays.asList(
			new Circle(), new Square(), new Triangle(), new Rhomboid()
		);
		// downcasting back to specific shape:
		for(Shape shape : shapeList)
			shape.draw();
		Rhomboid r = new Rhomboid();
		// Upcast:
		Shape s = (Shape)r;
		s.draw();
		// check type before downcast:
		if(s instanceof Circle) 
			((Circle)s).draw();
		else if(!(s instanceof Circle))
			System.out.println("(Shape)r is not a Circle");		
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?