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

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

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

[复制链接]
发表于 2011-6-12 14:58 | 显示全部楼层 |阅读模式
一共26题
 楼主| 发表于 2011-6-12 14:58 | 显示全部楼层
package exercises.Charpter10;

public class One {
        class Inner {
                private int i = 0;
                public Inner() {
                        System.out.println("Inner created!");
                }
                public int value() {return i;}
        }
        public Inner getInner(){
                return new Inner();
        }
       
        public static void main(String[] args){
                @SuppressWarnings("unused")
                One.Inner test = new One().getInner();
               
        }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-6-12 14:58 | 显示全部楼层
package exercises.Charpter10;

interface Selector {
        boolean end();
        Object current();
        void next();
}

class MyString{
        private String str = "";

        public MyString(String str) {
                this.str = str;
        }

        public String toString() {
                return str;
        }
}

public class Two {
        private Object[] items;
        private int next = 0;
       
        Two(int size){
                items = new Object[size];
        }
       
        public void add(Object x){
                if (next < items.length){
                        items[next++] = x;
                }
        }
       
        private class myStringSelector implements Selector{
                private int i = 0;
                public boolean end() {
                        return i == items.length;
                }

                public Object current() {
                        return items[i];
                }

                public void next() {
                        if(i < items.length){i++;}
                       
                }
               
        }
       
        public Selector selector(){
                return new myStringSelector();
        }
       
        public static void main(String[] args){
                Two sequence = new Two(10);
                for (int i = 0; i < 10; i++){
                        sequence.add(new MyString("test" + i));
                }
               
                Selector selector = sequence.selector();
                while (!selector.end()) {
                        System.out.println(selector.current().toString() + " ");
                        selector.next();
                }
        }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-6-12 14:58 | 显示全部楼层
package exercises.Charpter10;

public class Three {
        private String myStr = "";
        class Inner {
                private int i = 0;
                public Inner() {
                        System.out.println("Inner created!");
                }
                public int value() {return i;}
        }
        public Inner getInner(){
                return new Inner();
        }
       
        public Three(String x) {
                myStr = x;
        }
       
        public Three() {
        }
       
        class ForThree{
                public String toString(){
                        return myStr;
                }
        }
       
        public static void main(String[] args){
                Three test = new Three("Test");
                ForThree test2 = test.new ForThree();
                System.out.println(test2);
        }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-6-12 14:58 | 显示全部楼层
package exercises.Charpter10;

public class Four {
  private Object[] items;
  private int next = 0;
  public Four(int size) { items = new Object[size]; }
  public void add(Object x) {
    if(next < items.length)
      items[next++] = x;
  }
  class SequenceSelector implements Selector {
    private int i = 0;
    public boolean end() { return i == items.length; }
    public Object current() { return items[i]; }
    public void next() { if(i < items.length) i++; }
    public Four outter() {return Four.this;}
  }
  public Selector selector() {
    return new SequenceSelector();
  }       
  public static void main(String[] args) {
    Four sequence = new Four(11);
    for(int i = 0; i < 10; i++)
      sequence.add(Integer.toString(i));
    Selector selector = sequence.selector();
    Four newFour = ((SequenceSelector) selector).outter();
    newFour.add(Integer.toString(123));
    while(!selector.end()) {
      System.out.print(selector.current() + " ");
      selector.next();
    }
  }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-6-12 14:58 | 显示全部楼层
package exercises.Charpter10;

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

使用道具 举报

 楼主| 发表于 2011-6-12 14:59 | 显示全部楼层
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-6-12 15:00 | 显示全部楼层
package exercises.Charpter10;

public class Seven {
        private int a = 0;
        private void func(){
                System.out.println("Seven func");
        }
        class Inner{
                void myfunc(){
                        a = 10;
                        func();
                }
        }
        public static void main(String[] args){
                Seven test = new Seven();
                Inner test1 = test.new Inner();
                test1.myfunc();
                System.out.println(test.a);
               
        }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-6-12 15:00 | 显示全部楼层
package exercises.Charpter10;

public class Eight {
        @SuppressWarnings("unused")
        private class Inner{
                private int abc = 0;

                public void setAbc(int abc) {
                        this.abc = abc;
                }

                public int getAbc() {
                        return abc;
                }
        }
        //abc=1;不能用
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-6-12 15:00 | 显示全部楼层
package exercises.Charpter10;

interface Face{
        void func();
}

public class Nine {
        Face getFace() {
                class myFace implements Face{
                        public void func() {
                                System.out.println("My face!");
                        }
                }
                return new myFace();
        }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-6-12 15:00 | 显示全部楼层
package exercises.Charpter10;

public class Ten {
        Face getFace( boolean switcher) {
                if (switcher){
                        class myFace implements Face{
                                public void func() {
                                        System.out.println("My face!");
                                }
                        }
                        return new myFace();
                }else {
                        return null;
                }
        }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-6-12 15:00 | 显示全部楼层
package exercises.Charpter10;

interface Test1{
        void func();
}

public class Eleven {
        private class Test implements Test1{
                public void func(){
                        System.out.println();
                }
        }
        Test1 getTest1(){
                return new Test();
        }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-6-12 15:00 | 显示全部楼层
package exercises.Charpter10;

interface Inner{
        void myfunc();
}

public class Twelve {
        private int a = 0;
        private void func(){
                System.out.println("Twelve func");
        }
        public Inner returnInner(){
                return new Inner() {
                        public void myfunc(){
                                a = 10;
                                func();
                        }
                };
        }
       
        public static void main(String[] args){
                Twelve test = new Twelve();
                Inner test1 = test.returnInner();
                test1.myfunc();
                System.out.println(test.a);
        }
}

回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-6-12 15:00 | 显示全部楼层
package exercises.Charpter10;

public class Thirteen {
        Face getFace() {
                return new Face(){
                        public void func() {
                                System.out.println("My face!");
                        }
                };
        }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-6-12 15:00 | 显示全部楼层
package exercises.Charpter10;

//: interfaces/HorrorShow.java
// Extending an interface with inheritance.

interface Monster {
  void menace();
}

interface DangerousMonster extends Monster {
  void destroy();
}

interface Lethal {
  void kill();
}

class DragonZilla implements DangerousMonster {
  public void menace() {}
  public void destroy() {}
}       

interface Vampire extends DangerousMonster, Lethal {
  void drinkBlood();
}

class VeryBadVampire implements Vampire {
  public void menace() {}
  public void destroy() {}
  public void kill() {}
  public void drinkBlood() {}
}       

public class Fourteen {
  static void u(Monster b) { b.menace(); }
  static void v(DangerousMonster d) {
    d.menace();
    d.destroy();
  }
  static void w(Lethal l) { l.kill(); }
  public static void main(String[] args) {
    DangerousMonster barney = new DangerousMonster() {
               
                public void menace() {
                        System.out.println("DangerousMonster menace");
                }
               
                public void destroy() {
                        System.out.println("DangerousMonster destroy");
                }
        };
    u(barney);
    v(barney);
    Vampire vlad = new Vampire(){

                public void destroy() {
                        System.out.println("Vampire destroy");
                }

                public void menace() {
                        System.out.println("Vampire menace");
                }

                public void kill() {
                        System.out.println("Vampire kill");
                }

                public void drinkBlood() {
                        System.out.println("Vampire drinkBlood");
                }
           
    };
    u(vlad);
    v(vlad);
    w(vlad);
  }
} ///:~
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-6-12 15:00 | 显示全部楼层
package exercises.Charpter10;

class First{
        First(String me) {
                System.out.println("One.func " + me);
        }
}

class Second{
        public First getOne(){
                return new First("ou"){
                        {System.out.println("Two initialized. ");}
                };
        }
}

public class Fifteen {
        public static void main(String[] args){
                @SuppressWarnings("unused")
                First test = new Second().getOne();
        }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-6-12 15:00 | 显示全部楼层
package exercises.Charpter10;

interface Cycle{
        void ride();
}

interface CycleFactory{
        Cycle getCycle ();
}

class UnicycleFactory implements CycleFactory{

        public Cycle getCycle() {
                return new Cycle() {
                        public void ride() {
                                System.out.println("Unicycle ride");
                        }
                };
        }
}

class BicycleFactory implements CycleFactory{
        public Cycle getCycle() {
                return new Cycle() {
                        public void ride() {
                                System.out.println("Bicycle ride");
                        }
                };
        }
}

class TricycleFactory implements CycleFactory{
        public Cycle getCycle() {
                return new Cycle(){
                        public void ride() {
                                System.out.println("Tricycle ride");
                        }
                };
        }
}

public class Sixteen {
        public static void ride(CycleFactory myFactory){
                Cycle myCycle = myFactory.getCycle();
                myCycle.ride();
        }
       
        public static void main(String[] args){
                ride(new UnicycleFactory());
                ride(new BicycleFactory());
                ride(new TricycleFactory());
        }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-6-12 15:00 | 显示全部楼层
package exercises.Charpter10;

import java.util.Random;

interface Gamble{
        Boolean win();
}

interface GambleFactory{
        Gamble getGamble();
}

class CoinFactory implements GambleFactory{

        @Override
        public Gamble getGamble() {
                return new Gamble(){
                        @Override
                        public Boolean win() {
                                Random rand = new Random();
                                boolean win = rand.nextBoolean();
                                return win;
                        }
                };
        }
}

class DiceFactory implements GambleFactory{

        @Override
        public Gamble getGamble() {
                return new Gamble(){
                        @Override
                        public Boolean win() {
                                Random rand = new Random();
                                boolean win = rand.nextBoolean();
                                return win;
                        }
                };
        }
}

public class Seventeen {
        public static void playGamble(GambleFactory gambleFactorys){
                Gamble myGamble = gambleFactorys.getGamble();
                if (myGamble.win())
                        System.out.println("Win");
                else {
                        System.out.println("Lose");
                }
        }
       
        public static void main(String[] args){
                playGamble(new CoinFactory());
                playGamble(new DiceFactory());
        }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-6-12 15:01 | 显示全部楼层
package exercises.Charpter10;

public class Eighteen {
        public static class Test{
                void func(){
                        System.out.println("Test.func");
                }
        }
        public static void main(String[] args){
                Eighteen.Test testInner = new Eighteen.Test();
                testInner.func();
        }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-6-12 15:01 | 显示全部楼层
package exercises.Charpter10;

public class Nineteen {
        class Dot{
                class Comma{
                        void func(){
                                System.out.println("有没有这么玩的。");
                        }
                }
        }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-6-12 15:01 | 显示全部楼层
package exercises.Charpter10;

public interface Twenty {
        class MyClass implements Twenty{
                public static void main(String[] args){
                        @SuppressWarnings("unused")
                        MyClass test = new MyClass();
                        System.out.println("OK");
                }
        }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-6-12 15:01 | 显示全部楼层
package exercises.Charpter10;

import exercises.Charpter10.Test.MyTest;

interface Test{
        void func();
        class MyTest implements Test{

                @Override
                public void func() {
                        System.out.println("Test.func");
                }
               
                public static void display(Test myTest){
                        myTest.func();
                }
        }
}

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

使用道具 举报

 楼主| 发表于 2011-6-12 15:01 | 显示全部楼层
package exercises.Charpter10;

//: innerclasses/Sequence.java
// Holds a sequence of Objects.

public class Twentytwo {
  private Object[] items;
  private int next = 0;
  public Twentytwo(int size) { items = new Object[size]; }
  public void add(Object x) {
    if(next < items.length)
      items[next++] = x;
  }
  private class SequenceSelector implements Selector {
    private int i = 0;
    public boolean end() { return i == items.length; }
    public Object current() { return items[i]; }
    public void next() { if(i < items.length) i++; }
  }
  private class ReverseSelector implements Selector {
            private int i = items.length - 1;
            public boolean end() { return i == 0; }
            public Object current() { return items[i]; }
            public void next() { if(i > 0) i--; }
          }
  public Selector selector() {
    return new SequenceSelector();
  }
  public Selector reverseSelector() {
            return new ReverseSelector();
  }
  public static void main(String[] args) {
    Twentytwo sequence = new Twentytwo(10);
    for(int i = 0; i < 10; i++)
      sequence.add(Integer.toString(i));
    Selector selector = sequence.reverseSelector();
    while(!selector.end()) {
      System.out.print(selector.current() + " ");
      selector.next();
    }
  }
} /* Output:
0 1 2 3 4 5 6 7 8 9
*///:~
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-6-12 15:02 | 显示全部楼层
package exercises.Charpter10;

interface U{
        void f1(String x);
        void f2(String x);
        void f3(String x);
}

class A{
        U getU(){
                return new U(){

                        @Override
                        public void f1(String x) {
                                System.out.println(x + "A.ananymous f1");
                        }

                        @Override
                        public void f2(String x) {
                                System.out.println(x + "A.ananymous f2");
                        }

                        @Override
                        public void f3(String x) {
                                System.out.println(x + "A.ananymous f3");
                        }
                       
                };
        }
}

class B{
        U[] array = new U[10];
        int i = 0;
       
        public void addArray(U x){
                array[i++] = x;
        }
       
        public void clearArray(int p){
                if ((p >= 0) && (p < i)){
                        array[p] = null;
                }
        }
       
        public void all(){
                for (int j = 0; j < i; j++){
                        if (array[j] != null ){
                                array[j].f1(Integer.toString(j));
                                array[j].f2(Integer.toString(j));
                                array[j].f3(Integer.toString(j));
                        }
                }
        }
}

public class Twentythree {
        public static void main(String[] args){
                A generator = new A();
                B ans = new B();
                for (int i = 0; i < 10; i++){
                        ans.addArray(generator.getU());
                }
                ans.all();
                ans.clearArray(4);
                ans.all();
        }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-6-12 15:02 | 显示全部楼层
package exercises.Charpter10;

//: innerclasses/GreenhouseControls.java
// This produces a specific application of the
// control system, all in a single class. Inner
// classes allow you to encapsulate different
// functionality for each type of event.
import innerclasses.controller.*;

public class Twentyfour extends Controller {
  private boolean light = false;
  public class LightOn extends Event {
    public LightOn(long delayTime) { super(delayTime); }
    public void action() {
      // Put hardware control code here to
      // physically turn on the light.
      light = true;
    }
    public String toString() { return "Light is on"; }
  }       
  public class LightOff extends Event {
    public LightOff(long delayTime) { super(delayTime); }
    public void action() {
      // Put hardware control code here to
      // physically turn off the light.
      light = false;
    }
    public String toString() { return "Light is off"; }
  }
  private boolean water = false;
  public class WaterOn extends Event {
    public WaterOn(long delayTime) { super(delayTime); }
    public void action() {
      // Put hardware control code here.
      water = true;
    }
    public String toString() {
      return "Greenhouse water is on";
    }
  }       
  public class WaterOff extends Event {
    public WaterOff(long delayTime) { super(delayTime); }
    public void action() {
      // Put hardware control code here.
      water = false;
    }
    public String toString() {
      return "Greenhouse water is off";
    }
  }
  private boolean fan = false;
  public class FanOn extends Event {
    public FanOn(long delayTime) { super(delayTime); }
    public void action() {
      // Put hardware control code here to
      // physically turn on the light.
            fan = true;
    }
    public String toString() { return "Fan is on"; }
  }       
  public class FanOff extends Event {
    public FanOff(long delayTime) { super(delayTime); }
    public void action() {
      // Put hardware control code here to
      // physically turn off the light.
            fan = false;
    }
    public String toString() { return "Fan is off"; }
  }

  private String thermostat = "Day";       
  public class ThermostatNight extends Event {
    public ThermostatNight(long delayTime) {
      super(delayTime);
    }
    public void action() {
      // Put hardware control code here.
      thermostat = "Night";
    }
    public String toString() {
      return "Thermostat on night setting";
    }
  }       
  public class ThermostatDay extends Event {
    public ThermostatDay(long delayTime) {
      super(delayTime);
    }
    public void action() {
      // Put hardware control code here.
      thermostat = "Day";
    }
    public String toString() {
      return "Thermostat on day setting";
    }
  }
  // An example of an action() that inserts a
  // new one of itself into the event list:
  public class Bell extends Event {
    public Bell(long delayTime) { super(delayTime); }
    public void action() {
      addEvent(new Bell(delayTime));
    }
    public String toString() { return "Bing!"; }
  }       
  public class Restart extends Event {
    private Event[] eventList;
    public Restart(long delayTime, Event[] eventList) {
      super(delayTime);
      this.eventList = eventList;
      for(Event e : eventList)
        addEvent(e);
    }
    public void action() {
      for(Event e : eventList) {
        e.start(); // Rerun each event
        addEvent(e);
      }
      start(); // Rerun this Event
      addEvent(this);
    }
    public String toString() {
      return "Restarting system";
    }
  }       
  public static class Terminate extends Event {
    public Terminate(long delayTime) { super(delayTime); }
    public void action() { System.exit(0); }
    public String toString() { return "Terminating";  }
  }
} ///:~
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-6-12 15:02 | 显示全部楼层
package exercises.Charpter10;

import innerclasses.controller.Event;

public class Twentyfive extends Twentyfour{
          private boolean waterStrainer = false;
          public class WaterStrainerOn extends Event {
                    public WaterStrainerOn(long delayTime) { super(delayTime); }
                    public void action() {
                      // Put hardware control code here to
                      // physically turn on the light.
                            waterStrainer = true;
                    }
                    public String toString() { return "WaterStrainer is on"; }
          }
          public class WaterStrainerOff extends Event {
                    public WaterStrainerOff(long delayTime) { super(delayTime); }
                    public void action() {
                      // Put hardware control code here to
                      // physically turn off the light.
                            waterStrainer = false;
                    }
                    public String toString() { return "WaterStrainer is off"; }
          }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-6-12 15:02 | 显示全部楼层
package exercises.Charpter10;

class Final{
        class Inner{
                public Inner(String x) {
                        System.out.println(x);
                }
        }
}

public class Twentysix {
        class Inner extends Final.Inner{
                public Inner(Final final1, String x) {
                        final1.super(x);
                        System.out.println("in Twentysix Inner");
                }
        }
        public static void main(String[] args){
                @SuppressWarnings("unused")
                Inner test = new Twentysix().new Inner(new Final(), "Hello!");
        }
}
回复 支持 反对

使用道具 举报

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

GMT+8, 2023-12-7 18:20

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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