栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 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 shapes){
        for (RShape shape : shapes)
            if (!(shape instanceof RCircle))
                shape.rotate(45);
    }

    public static void main(String[] args) {
        List 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("===============");
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/820412.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号