Java - Thinking in Java 第7章 习题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012515223/article/details/47311057

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
 */
阅读更多

扫码向博主提问

SpikeKing

博客专家

士不可以不弘毅,任重而道远。
  • 擅长领域:
  • 深度学习
  • 计算机视觉
  • Android
去开通我的Chat快问
换一批

没有更多推荐了,返回首页