练习1:toy没有被初始化
练习2:不会
练习3:
import java.util.Arrays;
import java.util.List;
import java.util.stream.*;
abstract class Shape {
void draw(){
System.out.println(this + ".draw()");
}
@Override
public abstract String toString();
}
class Circle extends Shape{
@Override
public String toString() {
return "Circle";
}
}
class Square extends Shape{
@Override
public String toString() {
return "Square";
}
}
class Triangle extends Shape{
@Override
public String toString() {
return "Triangle";
}
}
class Rhomboid extends Shape{
@Override
public String toString() {
return "Rhomboid";
}
}
public class Shapes{
public static void main(String[] args) {
Stream.of(new Circle(),new Triangle(),new Square()).forEach(Shape::draw);
List<Shape> shapes = Arrays.asList(new Circle(),new Square(),new Triangle(),new Rhomboid());
for (Shape shape : shapes)
shape.draw();
Shape shape = new Rhomboid();
Rhomboid r = (Rhomboid) shape;
//Circle c = (Circle) shape;
}
}
练习4:
public class E04 {
public static void main(String[] args) {
Shape shape = new Rhomboid();
Rhomboid r = (Rhomboid) shape;
Circle c = null;
if (shape instanceof Circle)
c = (Circle) shape;
else
System.out.println("shape not a Circle");
}
}
练习5:
import java.util.Arrays;
import java.util.List;
abstract class RShape{
void draw(){
System.out.println(this + ".draw()");
}
abstract public String toString();
void rotate(int degrees){
System.out.println("Rotating " + this + " by " + degrees +" degrees");
}
}
class RCircle extends RShape{
@Override
public String toString() {
return "Circle";
}
void rotate(int degrees){
throw new UnsupportedOperationException();
}
}
class RSquare extends RShape {
@Override
public String toString() {
return "Square";
}
}
class RTriangle extends RShape {
@Override
public String toString() {
return "Triangle";
}
}
public class E05 {
static void rotateAll(List<RShape> shapes){
for (RShape shape : shapes)
if (!(shape instanceof RCircle))
shape.rotate(45);
}
public static void main(String[] args) {
List<RShape> shapes = Arrays.asList(new RCircle(),new RSquare(),new RTriangle());
rotateAll(shapes);
}
}
练习6:
class Cookie {
static { System.out.println("Loading Cookie"); }
}
class Gum {
static { System.out.println("Loading Gum"); }
}
class Candy {
static { System.out.println("Loading Candy"); }
}
public class SweetShop {
public static void main(String[] args) throws Exception{
for (String arg:args)
Class.forName(arg);
System.out.println("inside main");
new Candy();
System.out.println("After creating Candy");
try {
Class.forName("Gum");
} catch(ClassNotFoundException e) {
System.out.println("Couldn't find Gum");
}
System.out.println("After Class.forName(\"Gum\")");
new Cookie();
System.out.println("After creating Cookie");
}
}
练习8:
public class E08 {
static void printClass(Class<?> c){
if(c == null)return;
System.out.println(c.getName());
for(Class<?> k : c.getInterfaces()){
System.out.println("Interface: " + k.getName());
printClass(k.getSuperclass());
}
printClass(c.getSuperclass());
}
public static void main(String[] args) throws Exception{
for (int i = 0; i < args.length; i++) {
System.out.println("Displaying " + args[i]);
printClass(Class.forName(args[i]));
if (i < args.length - 1)
System.out.println("===============");
}
}
}