Java - Thinking in Java 第7章 习题

1

/**
 * 惰性初始化
 * <p/>
 * Created by wang on 15/8/6.
 */
class Init {
    public Init() {
        System.out.println("Init init");
    }
}

public class LazyInit {
    Init init;

    @Override
    public String toString() {
        System.out.println("Print");
        init = new Init();
        return super.toString();
    }

    public static void main(String[] arg) {
        System.out.println(new LazyInit());
    }
}
/**
 * Output:
 Print
 Init init
 LazyInit@a3901c6
 */

2

/**
 * 继承
 * <p/>
 * Created by wang on 15/8/7.
 */

class Cleaner {
    private String s = "Cleaner";

    public static void main(String[] args) {
        Cleaner x = new Cleaner();
        x.dilute().apply().scrub();
        System.out.println(x);
    }

    public void append(String a) {
        s += a;
    }

    public Cleaner dilute() {
        append(" dilute");
        return this;
    }

    public Cleaner apply() {
        append(" apply");
        return this;
    }

    public Cleaner scrub() {
        append(" scrub");
        return this;
    }

    @Override
    public String toString() {
        return s;
    }
}

class Detergent extends Cleaner {
    public static void main(String[] args) {
        Detergent d = new Detergent();
        ((Detergent) d.dilute().apply()).scrub().foam();
        System.out.println(d);
    }

    @Override
    public Detergent scrub() {
        append(" Detergent: scrub()");
        super.scrub();
        return this;
    }

    public Detergent foam() {
        append(" foam");
        return this;
    }
}

public class Disinfectant extends Detergent {
    public static void main(String[] args) {
        Disinfectant d = new Disinfectant();
        ((Disinfectant) d.dilute().apply()).scrub().sterilize();
        System.out.println(d);
        Detergent.main(args);
        Cleaner.main(args);
    }

    @Override
    public Disinfectant scrub() {
        append(" Disinfectant: scrub()");
        super.scrub();
        return this;
    }

    public Disinfectant sterilize() {
        append(" sterilize");
        return this;
    }
}
/**
 * Output:
 Cleaner dilute apply Disinfectant: scrub() Detergent: scrub() scrub sterilize
 Cleaner dilute apply Detergent: scrub() scrub foam
 Cleaner dilute apply scrub
 */

3

/**
 * 默认构造器
 * <p/>
 * Created by wang on 15/8/7.
 */
class Art {
    public void show() {
        System.out.println("show");
    }
}

class Drawing extends Art {
    public void draw() {
        System.out.println("draw");
    }
}

public class Cartoon extends Drawing {
    public void play() {
        System.out.println("play");
    }

    public static void main(String[] args) {
        Cartoon c = new Cartoon();
        c.show();
        c.draw();
        c.play();
    }
}

4

/**
 * 基类构造器
 * <p/>
 * Created by wang on 15/8/7.
 */
class Art {
    public Art(String author) {
        System.out.println("Art:" + author);
    }
}

class Drawing extends Art {
    public Drawing(String author) {
        super(author);
        System.out.println("Drawing:" + author);
    }
}

public class Cartoon extends Drawing {
    public Cartoon(String author) {
        super(author);
        System.out.println("Cartoon:" + author);
    }

    public static void main(String[] args) {
        new Cartoon("Caroline");
    }
}
/**
 * Output:
 Art:Caroline
 Drawing:Caroline
 Cartoon:Caroline
 */

5

/**
 * 构造函数先于其他函数调用.
 * <p/>
 * Created by wang on 15/8/7.
 */
class A {
    public A() {
        System.out.println("A");
    }
}

class B {
    public B() {
        System.out.println("B");
    }
}

class C extends A {
    private B mB = new B();
}

public class Cross {
    public static void main(String[] args) {
        C c = new C();
    }
}
/**
 * Output:
 A
 B
 */

6

报错: 对super的调用必须是构造函数中的第一个语句.

/**
 * 带参数的构造器
 * <p/>
 * Created by wang on 15/8/8.
 */
class Game {
    Game(int i) {
        System.out.println("Game Constructor:" + i);
    }
}

class BoardGame extends Game {
    BoardGame(int i) {
        super(i);
        System.out.println("BoardGame Constructor:" + i);
    }
}

public class Chess extends BoardGame {
    public Chess() {
        super(11);
        System.out.println("Chess Constructor");
//        super(11); // 报错
    }

    public static void main(String[] args) {
        new Chess();
    }
}
/**
 * 报错: 对super的调用必须是构造函数中的第一个语句
 */

7

/**
 * 继承带参数的构造器
 * <p/>
 * Created by wang on 15/8/7.
 */
class A {
    public A(String s) {
        System.out.println("A:" + s);
    }
}

class B {
    public B(String s) {
        System.out.println("B:" + s);
    }
}

class C extends A {
    public C(String s) {
        super(s);
    }

    private B mB = new B("Caroline");
}

public class Cross {
    public static void main(String[] args) {
        new C("Wendy");
    }
}
/**
 * Output:
 A:Wendy
 B:Caroline
 */

8

/**
 * 构造器
 * Created by wang on 15/8/8.
 */
class Haha {
    public Haha(String s) {
        System.out.println(s + ": Haha");
    }
}

public class DConstructor extends Haha {
    public DConstructor() {
        this("Caroline");
    }

    public DConstructor(String s) {
        super(s);
    }

    public static void main(String[] args) {
        new DConstructor();
    }
}
/**
 * Output:
 * Caroline: Haha
 */

9

/**
 * 派生类自动创建基类的成员变量
 * <p/>
 * Created by wang on 15/8/8.
 */
class Component1 {
    public Component1() {
        System.out.println("Component1");
    }
}

class Component2 {
    public Component2() {
        System.out.println("Component2");
    }
}

class Component3 {
    public Component3() {
        System.out.println("Component3");
    }
}

class Root {
    Component1 c1 = new Component1();
    Component2 c2 = new Component2();
    Component3 c3 = new Component3();
}

public class Stem extends Root {
    public static void main(String[] args) {
        new Stem();
    }
}

10

/**
 * 派生类自动创建基类的成员变量
 * <p/>
 * Created by wang on 15/8/8.
 */
class Component1 {
    public Component1(String s) {
        System.out.println("Component1:" + s);
    }
}

class Component2 {
    public Component2(String s) {
        System.out.println("Component2:" + s);
    }
}

class Component3 {
    public Component3(String s) {
        System.out.println("Component3:" + s);
    }
}

class Root {
    Component1 c1 ;
    Component2 c2 ;
    Component3 c3 ;

    public Root(String s1, String s2, String s3) {
        c1 = new Component1(s1);
        c2 = new Component2(s2);
        c3 = new Component3(s3);
    }


}

public class Stem extends Root {
    public Stem(String s1, String s2, String s3) {
        super(s1, s2, s3);
    }

    public static void main(String[] args) {
        new Stem("Hello", "Caroline", "Wendy");
    }
}
/**
 * Output:
 Component1:Hello
 Component2:Caroline
 Component3:Wendy
 */

11

/**
 * 继承
 * <p/>
 * Created by wang on 15/8/7.
 */

class Cleaner {
    private String s = "Cleaner";

    public static void main(String[] args) {
        Cleaner x = new Cleaner();
        x.dilute().apply().scrub();
        System.out.println(x);
    }

    public void append(String a) {
        s += a;
    }

    public Cleaner dilute() {
        append(" dilute");
        return this;
    }

    public Cleaner apply() {
        append(" apply");
        return this;
    }

    public Cleaner scrub() {
        append(" scrub");
        return this;
    }

    @Override
    public String toString() {
        return s;
    }
}

public class Detergent {

    private Cleaner c;

    public Detergent() {
        c = new Cleaner();
    }

    public static void main(String[] args) {
        Detergent d = new Detergent();
        d.dilute().apply().scrub();
        System.out.println(d);
    }

    public Detergent dilute() {
        c.append(" dilute");
        return this;
    }

    public Detergent apply() {
        c.append(" apply");
        return this;
    }

    public Detergent scrub() {
        c.append(" Detergent: scrub");
        return this;
    }

    @Override
    public String toString() {
        return c.toString();
    }
}

/**
 * Output:
 Cleaner dilute apply Detergent: scrub
 */

12

/**
 * 确保正确的清理
 * <p/>
 * Created by wang on 15/8/8.
 */
class Component1 {
    public Component1(String s) {
        System.out.println("Component1:" + s);
    }

    void dispose() {
        System.out.println("Dispose Component1");
    }
}

class Component2 {
    public Component2(String s) {
        System.out.println("Component2:" + s);
    }

    void dispose() {
        System.out.println("Dispose Component2");
    }
}

class Component3 {
    public Component3(String s) {
        System.out.println("Component3:" + s);
    }

    void dispose() {
        System.out.println("Dispose Component3");
    }
}

class Root {
    Component1 c1 ;
    Component2 c2 ;
    Component3 c3 ;

    public Root(String s1, String s2, String s3) {
        c1 = new Component1(s1);
        c2 = new Component2(s2);
        c3 = new Component3(s3);
    }

    void dispose() {
        c1.dispose();
        c2.dispose();
        c3.dispose();
        System.out.println("Dispose Root");
    }

}

public class Stem extends Root {
    public Stem(String s1, String s2, String s3) {
        super(s1, s2, s3);
    }

    @Override
    void dispose() {
        System.out.println("Dispose Stem");
        super.dispose();
    }

    public static void main(String[] args) {
        Stem s = new Stem("Hello", "Caroline", "Wendy");
        s.dispose();
    }
}
/**
 * Output:
 Component1:Hello
 Component2:Caroline
 Component3:Wendy
 Dispose Stem
 Dispose Component1
 Dispose Component2
 Dispose Component3
 Dispose Root
 */

13

/**
 * 继承重载
 * Created by wang on 15/8/17.
 */
class OneClass {
    void sayHello(String s) {
        System.out.println(s);
    }

    void sayHello(int i) {
        System.out.println(i + "-Hello");
    }

    void sayHello(float f) {
        System.out.println(f + "~Hello");
    }
}

class Doll {
    @Override
    public String toString() {
        return "Doll";
    }
}

class TwoClass extends OneClass {
    public void sayHello(Doll d) {
        System.out.println(d + " say Hello");
    }
}

public class OverloadClass {
    public static void main(String[] args) {
        TwoClass t = new TwoClass();
        t.sayHello(new Doll());
        t.sayHello("Caroline");
        t.sayHello(2);
        t.sayHello(0.5f);
    }
}
/**
 * Output:
 Doll say Hello
 Caroline
 2-Hello
 0.5~Hello
 */

14

/**
 * 使用组合处理对象
 * <p/>
 * Created by wang on 15/8/17.
 */
class Engine {
    public void start() {
        System.out.println("Start Engine");
    }

    public void rev() {
        System.out.println("Rev Engine");
    }

    public void stop() {
        System.out.println("Stop Engine");
    }

    public void service() {
        System.out.println("Service Engine");
    }
}

class Wheel {
    public void inflate(int psi) {
        System.out.println("Wheel psi-" + psi);
    }
}

class Window {
    public void rollUp() {
        System.out.println("Window roll up");
    }

    public void rollDown() {
        System.out.println("Window roll down");
    }
}

class Door {
    public Window window = new Window();

    public void open() {
        System.out.println("Window open");
    }

    public void close() {
        System.out.println("Window close");
    }
}

public class Car {
    public Engine engine = new Engine();
    public Wheel[] mWheels = new Wheel[4];
    public Door left = new Door(), right = new Door();

    {
        for (int i = 0; i < mWheels.length; ++i) {
            mWheels[i] = new Wheel();
        }
    }

    public static void main(String[] args) {
        Car c = new Car();
        c.engine.start();
        c.engine.rev();
        c.engine.service();
        c.engine.stop();
        c.mWheels[1].inflate(20);
        c.mWheels[3].inflate(25);
        c.left.window.rollUp();
        c.right.open();
    }
}
/**
 * Output:
 Start Engine
 Rev Engine
 Service Engine
 Stop Engine
 Wheel psi-20
 Wheel psi-25
 Window roll up
 Window open
 */

15

package access.local;

/**
 * protected方法类
 * Created by wang on 15/8/17.
 */
public class BabaoClass {
    protected void drink() {
        System.out.println("Babao Drink");
    }
}
package access.local;

/**
 * protected提供包内访问权限
 * Created by wang on 15/8/17.
 */
public class DrinkBabao {
    public static void main(String[] args) {
        BabaoClass b = new BabaoClass();
        b.drink(); // protected提供包内访问权限
    }
}
/**
 * Output:
 * Babao Drink
 */
package access.foreign;

import access.local.BabaoClass;

/**
 * 包外访问继承Protected
 * Created by wang on 15/8/17.
 */
public class LotusBabaoClass extends BabaoClass {
    public static void main(String[] args) {
        LotusBabaoClass l = new LotusBabaoClass();
        l.drink();
    }

    @Override
    protected void drink() {
        System.out.print("Lotus ");
        super.drink();
    }
}
/**
 * Output:
 Lotus Babao Drink
 */

16&17

/**
 * 向上转型
 * Created by wang on 15/8/18.
 */
class Amphibian {
    public void crawl(){
        System.out.println("Amphibian Crawl");
    }

    protected void color() {
        System.out.println("Amphibian Gray");
    }
}

class Frog extends Amphibian {
    @Override
    protected void color() {
        System.out.println("Frog Green");
    }
}

public class UpCastClass {
    static public void show(Amphibian a) {
        a.crawl();
        a.color();
    }

    public static void main(String[] args) {
        Frog frog = new Frog();
        show(frog);
    }
}
/**
 * Output:
 Amphibian Crawl
 Frog Green
 */

18

/**
 * final和static final
 * <p/>
 * Created by wang on 15/8/18.
 */
class Value {
    public int i;

    public Value(int i) {
        this.i = i;
    }

    @Override
    public String toString() {
        return i + "";
    }
}

class Corn {
    public static final Value SV = new Value(22);
    public final Value fv = new Value(11);
}

public class FinalTest {
    public static void main(String[] args) {
        Corn c1 = new Corn();
        Corn c2 = new Corn();

//        c1.fv = new Value(12); // 静态变量不变
        System.out.println(c1.SV.i = 20);
        System.out.println(c1.fv.i = 10);

        // 静态变量类共享
        System.out.println(c1.SV);
        System.out.println(c2.fv);
    }
}
/**
 * Output:
 20
 10
 20
 11
 */

19

/**
 * 空白final
 * <p/>
 * Created by wang on 15/8/18.
 */
public class EmptyFinal {
    public final int i;

    public EmptyFinal() {
        i = 12;
    }

    public EmptyFinal(int i) {
        this.i = i;
    }

    public static void main(String[] args) {
        EmptyFinal ef1 = new EmptyFinal();
        System.out.println(ef1.i);
//        ef.i = 10; // 无法为最终变量 i 指定值

        EmptyFinal ef2 = new EmptyFinal(2);
        System.out.println(ef2.i);
    }
}
/**
 * Output:
 12
 2
 */

20&21

覆盖final方法报错.

/**
 * 重载
 * <p/>
 * Created by wang on 15/8/20.
 */
class HahaClass {
    protected void f() {
        System.out.print("HAHA");
    }
    public final void dummy () {
        System.out.println("no voice");
    }
}

class HeheClass extends HahaClass {
    @Override
    protected void f() {
        super.f();
        System.out.println(" and HEHE");
    }

    protected void f(String s) {
        System.out.println(s + " say HEHE");
    }

//    @Override
//    public void dummy() {
//
//    }
}

public class OverrideClass {
    public static final void main(String[] args) {
        HeheClass h = new HeheClass();
        h.f();
        h.f("Caroline");
    }
}
/**
 * Output:
 (1)
 HAHA and HEHE
 Caroline say HEHE

 (2)
 覆盖final方法报错: HeheClass 中的 dummy() 无法覆盖 HahaClass 中的 dummy();被覆盖的方法为 final
 */

22

Error: 无法从最终 one.Insect 进行继承


23&24

package one;

/**
 * 继承初始化
 * <p/>
 * Created by wang on 15/8/21.
 */
class Insect {
    private static int si = numOfLegs("Static si Insect");
    protected int j;
    private int i = 0;

    public Insect() {
        // j并未初始化
        System.out.println("Insect Constructor: i = " + i + " j = " + j);
        j = 10;
    }

    protected static int numOfLegs(String s) {
        System.out.println(s);
        return 4;
    }
}

class Beetle extends Insect {
    private static int si = numOfLegs("Static si Beetle");
    private int i = numOfLegs("Beetle");

    // 并未显示调用Insect的构造函数
    public Beetle() {
        System.out.println("Beetle Constructor: i = " + i + " si = " + si);
    }
}

public class Cockroach extends Beetle {
    private static int si = numOfLegs("Static si Cockroach");
    private int i = numOfLegs("Cockroach");

    // 并未显示调用Insect的构造函数
    public Cockroach() {
        System.out.println("Cockroach Constructor: i = " + i + " si = " + si);
    }

    public static void main(String[] args) {
        System.out.println("Cockroach Construct");
//        Cockroach b = new Cockroach(); // 构造引起静态变量初始化
        System.out.println(Cockroach.si); // 调用静态变量
    }
}
/**
 * Output:
 (1) 构造初始化
 Static si Insect
 Static si Beetle
 Static si Cockroach
 Cockroach Construct
 Insect Constructor: i = 0 j = 0
 Beetle
 Beetle Constructor: i = 4 si = 4
 Cockroach
 Cockroach Constructor: i = 4 si = 4

 (2) 静态变量初始化
 Static si Insect
 Static si Beetle
 Static si Cockroach
 Cockroach Construct
 4
 */
写在前面的话 引言 1. 前提 2. Java的学习 3. 目标 4. 联机文档 5. 节 6. 练习 7. 多媒体 8. 源代码 9. 编码样式 10. Java版本 11. 课程和培训 12. 错误 13. 封面设计 14. 致谢 第1 对象入门 1.1 抽象的进步 1.2 对象的接口 1.3 实现方案的隐藏 1.4 方案的重复使用 1.5 继承:重新使用接口 1.5.1 改善基础类 1.5.2 等价和类似关系 1.6 多形对象的互换使用 1.6.1 动态绑定 1.6.2 抽象的基础类和接口 1.7 对象的创建和存在时间 1.7.1 集合与继承器 1.7.2 单根结构 1.7.3 集合库与方便使用集合 1.7.4 清除时的困境:由谁负责清除? 1.8 违例控制:解决错误 1.9 多线程 1.10 永久性 1.11 Java和因特网 1.11.1 什么是Web? 1.11.2 客户端编程 1.11.3 服务器端编程 1.11.4 一个独立的领域:应用程序 1.12 分析和设计 1.12.1 不要迷失 1.12.2 阶段0:拟出一个计划 1.12.3 阶段1:要制作什么? 1.12.4 阶段2:开始构建? 1.12.5 阶段3:正式创建 1.12.6 阶段4:校订 1.12.7 计划的回报 1.13 Java还是C++? 第2 一切都是对象 2.1 用句柄操纵对象 2.2 必须创建所有对象 2.2.1 保存在什么地方 2.2.2 特殊情况:主类型 2.2.3 Java中的数组 2.3 绝对不要清除对象 2.3.1 作用域 2.3.2 对象的作用域 2.4 新建数据类型:类 2.4.1 字段和方法 2.5 方法、自变量和返回值 2.5.1 自变量列表 2.6 构建Java程序 2.6.1 名字的可见性 2.6.2 使用其他组件 2.6.3 static关键字 2.7 我们的第一个Java程序 2.8 注释和嵌入文档 2.8.1 注释文档 2.8.2 具体语法 2.8.3 嵌入 2.8.4 @see:引用其他类 2.8.5 类文档标记 2.8.6 变量文档标记 2.8.7 方法文档标记 2.8.8 文档示例 2.9 编码样式 2.10 总结 2.11 练习 第3 控制程序流程 3.1 使用Java运算符 3.1.1 优先级 3.1.2 赋值 3.1.3 算术运算符 3.1.4 自动递增和递减 3.1.5 关系运算符 3.1.6 逻辑运算符 3.1.7 按位运算符 3.1.8 移位运算符 3.1.9 三元if-else运算符 3.1.10 逗号运算符 3.1.11 字串运算符 3.1.12 运算符常规操作规则 3.1.13 造型运算符 3.1.14 Java没有“sizeof” 3.1.15 复习计算顺序 3.1.16 运算符总结 3.2 执行控制 3.2.1 真和假 3.2.3 反复 3.2.6 中断和继续 3.2.7 切换 3.3 总结 3.4 练习 第4 初始化和清除 4.1 由构建器保证初始化 4.2 方法过载 4.2.1 区分过载方法 4.2.2 主类型的过载 4.2.3 返回值过载 4.2.4 默认构建器 4.2.5 this关键字 4.3 清除:收尾和垃圾收集 4.3.1 finalize()用途何在 4.3.2 必须执行清除 4.4 成员初始化 4.4.1 规定初始化 4.4.2 构建器初始化 4.5 数组初始化 4.5.1 多维数组 4.6 总结 4.7 练习 第5 隐藏实施过程 5.1 包:库单元 5.1.1 创建独一无二的包名 5.1.2 自定义工具库 5.1.3 利用导入改变行为 5.1.4 包的停用 5.2 Java访问指示符 5.2.1 “友好的” 5.2.2 public:接口访问 5.2.3 private:不能接触 5.2.4 protected:“友好的一种” 5.3 接口与实现 5.4 类访问 5.5 总结 5.6 练习 第6 类再生 6.1 合成的语法 6.2 继承的语法 6.2.1 初始化基础类 6.3 合成与继承的结合 6.3.1 确保正确的清除 6.3.2 名字的隐藏 6.4 到底选择合成还是继承 6.6 递增开发 6.7 上溯造型 6.7.1 何谓“上溯造型”? 6.8 final关键字 6.8.1 final数据 6.8.2 final方法 6.8.3 final类 6.8.4 final的注意事项 6.9 初始化和类装载 6.9.1 继承初始化 6.10 总结 6.11 练习 第7 多形性 7.1 上溯造型 7.1.1 为什么要上溯造型 7.2 深入理解 7.2.1 方法调用的绑定 7.2.2 产生正确的行为 7.2.3 扩展性 7.3 覆盖与过载 7.4 抽象类和
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ElminsterAumar

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值