北京交通大学论坛-知行信息交流平台

 找回密码
 注册
快速进入版块与发帖 搜索
查看: 4018|回复: 24

[作业贴] ThinkingInJava第四版第七章练习

[复制链接]
发表于 2011-5-20 14:02 | 显示全部楼层 |阅读模式
本帖最后由 lynx 于 2011-5-20 16:06 编辑

一共24题,按增序排列
 楼主| 发表于 2011-5-20 14:02 | 显示全部楼层
package exercises.Charpter7;

class SimpleClass{
        private char a;

        public void setA(char a) {
                this.a = a;
        }

        public char getA() {
                return a;
        }
}

class SecondClass{
        SimpleClass field;
}

public class One {
        public static void main(String[] args){
                SecondClass test = new SecondClass();
                test.field = new SimpleClass();
                test.field.setA('c');
                System.out.println(test.field.getA());
        }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-5-20 14:02 | 显示全部楼层
package exercises.Charpter7;

import reusing.Detergent;
import static net.mindview.util.Print.*;

class NewDetergent extends Detergent{
        public void scrub(){
                print("NewDetergent.scrub!");
        }
        public void strilize(){
                print("NewDetergent.strilize!");
        }
}

public class Two {
        public static void main(String[] args){
                NewDetergent test = new NewDetergent();
                test.scrub();
                test.strilize();
        }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-5-20 14:02 | 显示全部楼层
package exercises.Charpter7;

//: reusing/Cartoon.java
// Constructor calls during inheritance.
import static net.mindview.util.Print.*;

class Art {
  Art() { print("Art constructor"); }
}

class Drawing extends Art {
  //Drawing() { print("Drawing constructor"); }
}

public class Three extends Drawing {
  public Three() { print("Cartoon constructor"); }
  @SuppressWarnings("unused")
public static void main(String[] args) {
    Three x = new Three();
  }
} /* Output:
Art constructor
Cartoon constructor
*///:~
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-5-20 14:02 | 显示全部楼层
package exercises.Charpter7;

//: reusing/Cartoon.java
// Constructor calls during inheritance.
//第三题就验证了第四题
import static net.mindview.util.Print.*;

public class Four extends Drawing {
  public Four() { print("Cartoon constructor"); }
  @SuppressWarnings("unused")
public static void main(String[] args) {
    Four x = new Four();
  }
} /* Output:
Art constructor
Cartoon constructor
*///:~
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-5-20 14:02 | 显示全部楼层
package exercises.Charpter7;

import static net.mindview.util.Print.*;

class A{
        public A() {
                print("A construct.");
        }
}

class B{
        public B() {
                print("B construct.");
        }
}

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

public class Five {
        @SuppressWarnings("unused")
        public static void main(String[] args){
                C test = new C();
        }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-5-20 14:02 | 显示全部楼层
package exercises.Charpter7;

//: reusing/Chess.java
// Inheritance, constructors and arguments.
import static net.mindview.util.Print.*;

class Game {
  Game(int i) {
    print("Game constructor");
  }
}

class BoardGame extends Game {
  BoardGame(int i) {
    super(i);
    print("BoardGame constructor");
  }
}       

public class Six extends BoardGame {
  Six() {
    super(11);
    print("Chess constructor");
  }
  @SuppressWarnings("unused")
public static void main(String[] args) {
    Six x = new Six();
  }
} /* Output:
Game constructor
BoardGame constructor
Chess constructor
*///:~
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-5-20 14:02 | 显示全部楼层
package exercises.Charpter7;

import static net.mindview.util.Print.*;

class NewA{
        NewA(int i){
                print("A construct");
        }
        NewA(){
                print("A construct without args");
        }
}

class NewB{
        NewB(int i){
                print("B construct");
        }
}

class NewC extends NewA{
        NewB test = new NewB(0);
        NewC(){
                super(0);
        }
}

public class Seven {
        @SuppressWarnings("unused")
        public static void main(String[] args){
                NewC test = new NewC();
        }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-5-20 14:03 | 显示全部楼层
package exercises.Charpter7;

class TestEight extends NewB{
        TestEight(){
                super(0);
        }
        TestEight(int i){
                super(i);
        }
}

public class Eight {
        @SuppressWarnings("unused")
        public static void main(String[] args){
                TestEight test = new TestEight(1);
                test = new TestEight();
        }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-5-20 14:03 | 显示全部楼层
package exercises.Charpter7;

import static net.mindview.util.Print.*;

class Component1{
        Component1(){
                print("Component1 constructed!");
        }
}

class Component2{
        Component2(){
                print("Component2 constructed!");
        }
}

class Component3{
        Component3(){
                print("Component3 constructed!");
        }
}

class root{
        Component1 a;
        Component2 b;
        Component3 c;
        root(){
                a = new Component1();
                b = new Component2();
                c = new Component3();
                print("root constructed!");
        }
}

class Stem extends root{
        Stem(){
                print("Stem constructed!");
        }
}

public class Nine {
        @SuppressWarnings("unused")
        public static void main(String[] args){
                Stem test = new Stem();
        }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-5-20 14:03 | 显示全部楼层
package exercises.Charpter7;

import static net.mindview.util.Print.*;

class NewComponent1{
        NewComponent1(int i){
                print("NewComponent1 constructed!");
        }
}

class NewComponent2{
        NewComponent2(int i){
                print("NewComponent2 constructed!");
        }
}

class NewComponent3{
        NewComponent3(int i){
                print("NewComponent3 constructed!");
        }
}

class Newroot{
        NewComponent1 a;
        NewComponent2 b;
        NewComponent3 c;
        Newroot(int i){
                a = new NewComponent1(i);
                b = new NewComponent2(i);
                c = new NewComponent3(i);
                print("Newroot constructed!");
        }
}

class NewStem extends Newroot{
        NewStem(int i){
                super(i);
                print("NewStem constructed!");
        }
}

public class Ten {
        @SuppressWarnings("unused")
        public static void main(String[] args){
                NewStem test = new NewStem(0);
        }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-5-20 14:03 | 显示全部楼层
package exercises.Charpter7;

//: reusing/Detergent.java
// Inheritance syntax & properties.
import static net.mindview.util.Print.*;

class Cleanser {
  private String s = "Cleanser";
  public void append(String a) { s += a; }
  public void dilute() { append(" dilute()"); }
  public void apply() { append(" apply()"); }
  public void scrub() { append(" scrub()"); }
  public String toString() { return s; }
  public static void main(String[] args) {
    Cleanser x = new Cleanser();
    x.dilute(); x.apply(); x.scrub();
    print(x);
  }
}       

public class Eleven {
  // Change a method:
        Cleanser test = new Cleanser();
  public void scrub() {
    test.append(" Detergent.scrub()");
    test.scrub(); // Call base-class version
  }
  // Add methods to the interface:
  public void foam() { test.append(" foam()"); }
  // Test the new class:
  public static void main(String[] args) {
    Eleven x = new Eleven();
    x.test.dilute();
    x.test.apply();
    x.scrub();
    x.foam();
    print(x);
    print("Testing base class:");
    Cleanser.main(args);
  }       
} /* Output:
Cleanser dilute() apply() Detergent.scrub() scrub() foam()
Testing base class:
Cleanser dilute() apply() scrub()
*///:~
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-5-20 14:03 | 显示全部楼层
package exercises.Charpter7;

import static net.mindview.util.Print.*;

class DisposeComponent1{
        DisposeComponent1(){
                print("DisposeComponent1 constructed!");
        }
        void dispose(){
                print("DisposeComponent1 Disposed");
        }
}

class DisposeComponent2{
        DisposeComponent2(){
                print("DisposeComponent2 constructed!");
        }
        void dispose(){
                print("DisposeComponent2 Disposed");
        }
}

class DisposeComponent3{
        DisposeComponent3(){
                print("DisposeComponent3 constructed!");
        }
        void dispose(){
                print("DisposeComponent3 Disposed");
        }
}

class Disposeroot{
        DisposeComponent1 a;
        DisposeComponent2 b;
        DisposeComponent3 c;
        Disposeroot(){
                a = new DisposeComponent1();
                b = new DisposeComponent2();
                c = new DisposeComponent3();
                print("Disposeroot constructed!");
        }
        void dispose(){
                print("Disposeroot Disposed");
                a.dispose();
                b.dispose();
                c.dispose();
        }
}

class DisposeStem extends Disposeroot{
        DisposeStem(){
                print("DisposeStem constructed!");
        }
        void dispose(){
                print("DisposeStem Disposed");
                super.dispose();
        }
}

public class Twelve {
        public static void main(String[] args){
                DisposeStem test = null;
                try{
                        test = new DisposeStem();
                } finally{
                        test.dispose();
                }
        }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-5-20 14:03 | 显示全部楼层
package exercises.Charpter7;

class Override{
        void func(int i){
                System.out.println("Override");
        }
}

class Override1 extends Override{
        void func(char i){
                System.out.println("Override1");
        }
}

class Override2 extends Override1{
        void func(String i){
                System.out.println("Override2");
        }
}

public class Thirteen{
        public static void main(String[] args){
                Override2 test = new Override2();
                test.func(1);
                test.func('a');
                test.func("s");
        }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-5-20 14:03 | 显示全部楼层
package exercises.Charpter7;

//: reusing/Car.java
// Composition with public objects.

class Engine {
  public void start() {}
  public void rev() {}
  public void stop() {}
  public void service() {System.out.println("OK");}
}

class Wheel {
  public void inflate(int psi) {}
}

class Window {
  public void rollup() {}
  public void rolldown() {}
}

class Door {
  public Window window = new Window();
  public void open() {}
  public void close() {}
}

public class Fourteen {
  public Engine engine = new Engine();
  public Wheel[] wheel = new Wheel[4];
  public Door
    left = new Door(),
    right = new Door(); // 2-door
  public Fourteen() {
    for(int i = 0; i < 4; i++)
      wheel[i] = new Wheel();
  }
  public static void main(String[] args) {
    Fourteen car = new Fourteen();
    car.left.window.rollup();
    car.wheel[0].inflate(72);
    car.engine.service();
  }
} ///:~
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-5-20 14:03 | 显示全部楼层
package exercises.Charpter7;

import reusing.Orc;

public class Fifteen extends Orc{
        public Fifteen(String name, int orcNumber) {
                super(name, orcNumber);
        }

        public static void main(String[] args){
                Orc orc = new Orc("Limburger", 12);
                System.out.println(orc);
                orc.change("Bob", 19);
                System.out.println(orc);
                orc.change("Bob", 19);
        }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-5-20 14:03 | 显示全部楼层
package exercises.Charpter7;

class Amphibian{
        private String name = null;
       
        Amphibian(){
                setName("");
        }
       
        Amphibian(String name){
                setName(name);
        }
       
        void move(){
               
        }
        void love(Amphibian other){
                System.out.println(getName() + " loves " + other.getName());
        }

        public void setName(String name) {
                this.name = name;
        }

        public String getName() {
                return name;
        }
}

class Frog extends Amphibian{
        Frog(String name){
                super(name);
        }
       
}

public class Sixteen {
        public static void main(String[] args){
                Frog a = new Frog("testa");
                Frog b = new Frog("testb");
                a.love(b);
        }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-5-20 14:03 | 显示全部楼层
package exercises.Charpter7;

class NewFrog extends Amphibian{
        NewFrog(String name){
                super(name);
        }
       
        void love(Amphibian other){
                System.out.println(getName() + " loves " + other.getName() + "?");
        }
}

public class Seventeen {
        public static void main(String[] args){
                NewFrog a = new NewFrog("testa");
                NewFrog b = new NewFrog("testb");
                a.love(b);
        }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-5-20 14:03 | 显示全部楼层

package exercises.Charpter7;

class TestFinal{
        static final int a = 0;
        final int b;
        TestFinal(int i){
                b = i;
        }
        TestFinal(){
                b = 1;
        }
        public String toString() {
                return a + " & " + b;
        }
}

public class Eighteen {
        public static void  main(String[] args) {
                TestFinal test1 = new TestFinal(10);
                TestFinal test2 = new TestFinal(11);
                System.out.println(test1);
                System.out.println(test2);
        }
}

回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-5-20 14:04 | 显示全部楼层
package exercises.Charpter7;

public class Nineteen {
        public static void  main(String[] args) {
                TestFinal test1 = new TestFinal(20);
                TestFinal test2 = new TestFinal(300);
                System.out.println(test1);
                System.out.println(test2);
        }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-5-20 14:04 | 显示全部楼层
package exercises.Charpter7;

class TestOverride{
        protected String Test(){
                System.out.println("TestOverride!");
                return "TestOverride!";
        }
        final public void Test(String str){
                System.out.println("TestOverride!" + str);
        }
}

class Tests extends TestOverride{
        //@Override//注解不能用
        protected String Test(){
                System.out.println("Test!");
                return "Test!";
        }
//        public void Test(String str){//不能覆盖
//                System.out.println("TestOverride!" + str);
//        }
}

public class Twenty{
        public static void main(String[] args){
                Tests a = new Tests();
                a.Test();
        }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-5-20 14:04 | 显示全部楼层
package exercises.Charpter7;

class Tests1 extends TestOverride{
        //@Override//注解不能用
        protected String Test(){
                System.out.println("Test!");
                return "Test!";
        }
//        public void Test(String str){//不能覆盖
//                System.out.println("TestOverride!" + str);
//        }
}

public class Twentyone{
        public static void main(String[] args){
                Tests1 a = new Tests1();
                a.Test();
        }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-5-20 14:04 | 显示全部楼层
package exercises.Charpter7;

final class TestFinal1{
       
}

public class Twentytwo{// 无法extends TestFinal1

}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-5-20 16:06 | 显示全部楼层
package exercises.Charpter7;

class TestLoad{
        TestLoad(){
                System.out.println("TestLoad Initialized");
        }
        static int i;
        static{
                System.out.println("Static initialized!");
                i = 0;
        }
}

public class Twentythree {
        public static void main(String[] args){
                System.out.println(TestLoad.i);
                TestLoad test = new TestLoad();
        }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-5-20 16:07 | 显示全部楼层
package exercises.Charpter7;

//: reusing/Beetle.java
//The full process of initialization.
import static net.mindview.util.Print.*;

class Insect {
private int i = 9;
protected int j;
Insect() {
  print("i = " + i + ", j = " + j);
  j = 39;
}
private static int x1 =
  printInit("static Insect.x1 initialized");
static int printInit(String s) {
  print(s);
  return 47;
}
}

class Beetle extends Insect {
private int k = printInit("Beetle.k initialized");
public Beetle() {
  print("k = " + k);
  print("j = " + j);
}
private static int x2 =
  printInit("static Beetle.x2 initialized");
} /* Output:
static Insect.x1 initialized
static Beetle.x2 initialized
Beetle constructor
i = 9, j = 0
Beetle.k initialized
k = 47
j = 39
*///:~

public class Twentyfour extends Beetle{
        public Twentyfour() {
                super();
        }
        public static void main(String[] args){
                Twentyfour test = new Twentyfour();
        }
}
回复 支持 反对

使用道具 举报

手机版|北京交通大学论坛-知行信息交流平台 ( BJTUICP备13011901号 )

GMT+8, 2023-11-30 02:11

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表