JAVA编程思想第十四章练习题

 练习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("===============");
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值