Java-Java编程思想第四版 第十章 练习

xiaoxiao2021-02-28  41

练习1: /* Write a class named Outer that contains an inner class named Innet. * Add a method to Outer that returns an object of type Inner. In main(), * create and initialize a reference to an Inner. */ import static net.mindview.util.Print.*; class Outer{ Inner out(){return new Inner();} class Inner{ void in(){print("it's inner class");} } } public class Ja10_1_1{ public static void main(String[] args){ Outer ou=new Outer(); Outer.Inner in=ou.out(); in.in(); } }

练习2:

/* Create a class that holds a String, and has a toString() method that * displays this String. Add several instances of your new class to a * Sequence ojbect, then display them. */ import static net.mindview.util.Print.*; interface Selector{ boolean end(); void next(); Object current(); } class Word{ private String word;// this "word" can't be static. Or, the value can't be assigned! public Word(String s){word=s;} public String toString(){return word;}//toString must be public } public class Ja10_2_2{ private Object[] aa;//it must be Object private static int i=0; private static int j=0; public Ja10_2_2(int size){aa=new Object[size];} //the size of Number array must be defined here. private class SequenceSelector implements Selector{ public Object current(){return aa[j];} public void next(){j++;} public boolean end(){if(j<aa.length) return true; else{j=0;return false;} } } Selector selector(){return new SequenceSelector();} void add(Object s){//it must be Object if(i<aa.length){ aa[i]=s; print(aa[1]); ++i; } } public static void main(String[] args){ Ja10_2_2 ja=new Ja10_2_2(3); ja.add(new Word("aaaa")); ja.add(new Word("bbbb")); ja.add(new Word("cccc")); Selector sel=ja.selector(); while(sel.end()){ print(sel.current()); sel.next(); } } }

PS:(1)变量aa必须定义成Object,这样导入参数时Word类可以向上转型成Object,使toString()可以工作(2)toString()方法必须是public的(3)String word变量不能是static的,否则当它从aaaa变到cccc,指向它的a[0]~a[2]的数组的值会全部变成cccc!(4)数组的大小必须事前定义好(Ja-22-),否则会wei'l

练习3:

/* Modify Exercise 1 so that Outer has a private String field (initialized * by the constructor), and Inner has a toString() that displays this field. * Create an object of type Inner and display it. */ import static net.mindview.util.Print.*; class Outer{ private String ss; Inner out(){ss="dsaf"; return new Inner();} class Inner{ public String toString(){return ss;} void in(){print("it's inner class");} } } public class Ja10_2_3{ public static void main(String[] args){ Outer ou=new Outer(); Outer.Inner in=ou.out(); print(in); } }

练习4:

import static net.mindview.util.Print.*; interface Selector { boolean end(); Object current(); void next(); Ja10_3_4 infer();//! } public class Ja10_3_4 { private Object[] items; private int next = 0; public Ja10_3_4(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++; } public Ja10_3_4 infer(){return Ja10_3_4.this;}//! } public Selector selector() { return new SequenceSelector(); } public void outerInferred(){print("Ja10_3_4 inferred.");} public static void main(String[] args) { Ja10_3_4 sequence = new Ja10_3_4(10); for(int i = 0; i < 10; i++) sequence.add(Integer.toString(i)); Selector selector = sequence.selector(); selector.infer().outerInferred();//! while(!selector.end()) { System.out.print(selector.current() + " "); selector.next(); } } }

PS://!处,为新增代码

练习5:

import static net.mindview.util.Print.*; class Outer{ class Inner{ Inner(){print("adsf");} } } public class Ja10_3_5{ public static void main(String[] args){ Outer ou=new Outer(); Outer.Inner in=ou.new Inner(); } }

练习6:

import net.mindview.simple.*; /*interface Ja10_4_6a{ void f(); }*/ /*class Ja10_4_6b{ class Ja10_4_6c implements Ja10_4_6a{ public void f(){System.out.println("It's Ja10_4_6b");} } }*/ public class Ja10_4_6 extends Ja10_4_6b{ public static void main(String[] args){ Ja10_4_6b ja=new Ja10_4_6b(); Ja10_4_6a jac=ja.new Ja10_4_6c(); jac.f(); } }

练习7:

import static net.mindview.util.Print.*; class Ja10_4_7a{ private int i=5; private void outMethod(){print("It's method of outer");} class Ja10_4_7b{ void f(){i=3;print(i);} void inMethod(){outMethod();} } } public class Ja10_4_7{ public static void main(String[] args){ Ja10_4_7a jaa=new Ja10_4_7a(); Ja10_4_7a.Ja10_4_7b jab=jaa.new Ja10_4_7b(); jab.f(); jab.inMethod(); } }

练习9:

/* Create an interface with at least one method, and implement that * interface by defining an inner class within a method, which returns a * reference to your interface. */ import static net.mindview.util.Print.*; interface A{ void f(); } public class Ja10_5_9{ A outMethod1(){ class B implements A{ public void f(){ print("it's B implements A.f()"); } } return new B(); } public static void main(String[] args){ Ja10_5_9 ja=new Ja10_5_9(); A a=ja.outMethod1(); a.f(); } }

练习11:

/* Create a private inner class that implements a public interface. * Write a method that returns a reference to an instance of the private * inner class, upcast to the interface. Show that the inner class is * completely hidden by trying to downcast to it. */ import static net.mindview.util.Print.*; interface A{void f();} /*public class Ja10_5_11{ A forReturn(){ private class B implements A{ public void f(){print("It's Ja10_5_11.B.f()");} } return new B(); } public static void main(String[] args){ Ja10_5_11 ja=new Ja10_5_11(); A a=ja.forReturn(); a.f(); } }*/ class C{ private class B implements A{ public void f(){print("It's Ja10_5_11.B.f()");} } A forReturn(){return new B();} } public class Ja10_5_11{ public static void main(String[] args){ C c=new C(); A a=c.forReturn(); a.f(); //!B b=(B)a; //!b.f(); } }

PS: 方法内部的内部类,不能设成private?

练习12:

/* Repeat Exercise 7 using an anonymous inner class. * (Exercise 7: Create a class with a private field and a private method. * Create an inner class with a method that modifies the outer-class field * and calls the outer class method. In a second outer-class method, create * an object of the inner class and call its method, then show the effect on * the outer-class object.) */ import static net.mindview.util.Print.*; interface Ja10_4_7b{ void f(); void inMethod(); } class Ja10_4_7a{ private int i=5; private void outMethod(){print("It's method of outer");} Ja10_4_7b bb(){ return new Ja10_4_7b(){ private int j=5; private void bb(){print(j);} {print(j);} public void f(){i=3;print(i);} public void inMethod(){outMethod();} }; } } public class Ja10_6_12{ public static void main(String[] args){ Ja10_4_7a jaa=new Ja10_4_7a(); jaa.bb(); } }

练习13:

/* Repeat Exercise 9 using an anonymous inner class. * (Exercise 9: Create an interface with at least one method, and implement * that interface by defining an inner class within a method, which returns * a reference to your interface.) */ import static net.mindview.util.Print.*; interface A{ void f(); } public class Ja10_6_13{ A outMethod1(){ return new A(){ {print("it's inner class A");} public void f(){ print("it's B implements A.f()"); } }; } public static void main(String[] args){ Ja10_6_13 ja=new Ja10_6_13(); A a=ja.outMethod1(); a.f(); } }

练习14:

/* Modify interfaces/HorrorShow.java to implement DangerousMonster and * Vampire using anonymous classes. */ interface Monster { void menace(); } interface DangerousMonster extends Monster { void destroy(); } interface Lethal { void kill(); } interface Vampire extends DangerousMonster, Lethal { void drinkBlood(); } public class Ja10_6_14 { static void u(Monster b) { b.menace(); } static void v(DangerousMonster d) { d.menace(); d.destroy(); } static void w(Lethal l) { l.kill(); } DangerousMonster monster(){ return new DangerousMonster(){ public void menace() {} public void destroy() {} }; } Vampire vampire(){ return new Vampire(){ public void menace() {} public void destroy() {} public void kill() {} public void drinkBlood() {} }; } public static void main(String[] args) { Ja10_6_14 ja=new Ja10_6_14(); DangerousMonster barney =ja.monster(); u(barney); v(barney); Vampire vlad =ja.vampire(); u(vlad); v(vlad); w(vlad); } }

练习15:

/* Create a class with a non-default constructor and no default constructor. * Create a second class that has a method that returns a reference to an * object of the first class. Create the object that you return by making an * anonymous inner class that inherits from the first class. */ import static net.mindview.util.Print.*; abstract class A{ void a(int i){} } class C extends A{} class B{ A returnInner(final int i){ return new C(){ //private int ii=i; void a(int ii){print("It's B.C.a("+ii+")");} }; } } public class Ja10_6_15{ public static void main(String[] args){ B b=new B(); b.returnInner(5).a(8); } }

练习16:

/* Modify the solution to Exercise 18 from the Interfaces chapter to use * anonymous inner classes. * (Exercise 18, Interface: Create a Cycle interface, with implementations * Unicycle, Bicycle and Tricycle. Create factories for each type of Cycle, * and code that uses these factories. */ import static net.mindview.util.Print.*; interface Cycle{void produce();} interface CycleFactory{Cycle getCycle();} class Unicycle implements Cycle{ public void produce(){print("it's Unicycle");} public static CycleFactory factory=new CycleFactory(){ public Cycle getCycle(){return new Unicycle();} }; } class Bicycle implements Cycle{ public void produce(){print("it's Bicycle");} public static CycleFactory factory=new CycleFactory(){ public Cycle getCycle(){ return new Bicycle(); } }; } class Tricycle implements Cycle{ public void produce(){print("it's Tricycle");} public static CycleFactory factory=new CycleFactory(){ public Cycle getCycle(){return new Tricycle();} }; } public class Ja10_6_16{ static void make(CycleFactory cf){ Cycle c=cf.getCycle(); c.produce(); } public static void main(String[] args){ make(Unicycle.factory); make(Bicycle.factory); make(Tricycle.factory); } }

练习18:

/* Create a class containing a nested class. In main(), create an instance of * the nested class. */ class C{ class D{} } public class Ja10_6_18{ static class B{ } class A{} public static void main(String[] args){ B b=new B(); //!A a=new A(); Ja10_6_18 ja=new Ja10_6_18(); A a=ja.new A(); C c=new C(); C.D d=c.new D(); } }

练习19:

/* Create a class containing an inner class that itself contains an inner * class. Repeat this using nested classes. Note the names of the .class files * produced by the compiler. */ import static net.mindview.util.Print.*; public class Ja10_6_19{ private static class A{ private static class B{ void f(){print("It's Ja10_6_19.A.B");} } } public static void main(String[] args){ A.B b=new A.B(); b.f(); } }

练习20:

/* Create an interface containing a nested class. Implement this interface and * create an instance of the nested class. */ import static net.mindview.util.Print.*; public interface Ja10_7_20{ void f(); class B implements Ja10_7_20{ public void f(){print("sds");} public static void main(String[] args){ B b=new B(); b.f(); } } }

练习21:

/* Create an interface that contains a nested class that has a static method that * calls the methods of your interface and displays the results. Implement your * interface and pass an instance of your implementation to the method. */ import static net.mindview.util.Print.*; interface A{ public void f(); public static class B{ static void ff(Ja10_7_21 i){ print(i); } } } public class Ja10_7_21 implements A{ public void f(){print("sooos");} public static void main(String[] args){ Ja10_7_21 ja=new Ja10_7_21(); A.B.ff(ja); } }

PS:知道了调用方法ff(),只需引用A.B.ff()。B和f()都是静态的

练习22:

interface Selector { boolean end(); Object current(); void next(); } public class Ja10_8_22{ private Object[] items; private int next = 0; public Ja10_8_22(int size) { items = new Object[size]; } public void add(Object x) { if(next < items.length) items[next++] = x; } private class SequenceSelector{ Selector forwardSelector(){ return new 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++; } }; } Selector reverseSelector(){ return new Selector() { private int i = items.length-1; public boolean end() { return i == -1; } public Object current() { return items[i]; } public void next() { if(i > -1) i--; }//[0,length-1] }; } } public Selector selector(int i) { if(i==1){return new SequenceSelector().forwardSelector();} else{return new SequenceSelector().reverseSelector();} } public static void main(String[] args) { Ja10_8_22 sequence = new Ja10_8_22(10); for(int i = 0; i < 10; i++) sequence.add(Integer.toString(i)); Selector selector = sequence.selector(0); while(!selector.end()) { System.out.print(selector.current() + " "); selector.next(); } } }

练习23:

/* Create an interface U with three methods. Create a class A with a method that * produces a reference to a U by building an anonymous inner class. Create a second * class B that contains an array of U. B should have one method that accepts and * stores a reference to U in the array, a second method that sets a reference in * the array (specified by the method argument) to null, and a third method that * moves through the array and calls the methods in U. In main, create a group of A * objects and a single B. Fill the B with U references produced by the A objects. * Use the B to call back into all the A objects. Remove some of the U references * from the B. */ import static net.mindview.util.Print.*; interface U{void a();void b();void c();} class A{ U aA(){ return new U(){ public void a(){print("A.a()");} public void b(){print("A.b()");} public void c(){print("A.c()");} }; } } class B{ private U[] us; B(int i){us=new U[i];} void addU(U u, int i){ us[i]=u; } void eraseU(int i){us[i]=null;} void testUs(){for(U u:us){u.a(); u.b(); u.c();}} } public class Ja10_8_23{ public static void main(String[] args){ B bb=new B(5); for(int i=0;i<5;i++){ A aa=new A(); bb.addU(aa.aA(),i); } //bb.testUs(); bb.eraseU(3); bb.eraseU(1); bb.testUs(); } }

练习24:

/* In GreenhouseControls.java, add Event inner classes that turn fans on and * off. Configure GreenhouseController.java to use these new Event objects. */ import innerclasses.*; import innerclasses.controller.*; public class Ja10_8_24 { public static void main(String[] args) { GreenhouseControls gc = new GreenhouseControls(); // Instead of hard-wiring, you could parse // configuration information from a text file here: gc.addEvent(gc.new Bell(900)); Event[] eventList = { gc.new ThermostatNight(0), gc.new LightOn(200), gc.new LightOff(400), gc.new WaterOn(600), gc.new WaterOff(800), gc.new ThermostatDay(0), gc.new FanOn(0), gc.new FanOff(0) }; gc.addEvent(gc.new Restart(2000, eventList)); gc.addEvent( new GreenhouseControls.Terminate( 2000)); gc.run(); } } /* Output *///:~

练习25:

/* Inherit from GreenhouseControls in GreenhouseControls.java to add Event * inner classes that turn water mist generators on and off. Write a new * version of GreenhouseController.java to use these new Event objects. */ import innerclasses.*; import innerclasses.controller.*; class GreenhouseControls2 extends GreenhouseControls{ private boolean jet=false; public class JetOn extends Event{ public JetOn(long delayTime){super(delayTime);} public void action(){jet=true;} public String toString(){return "Jet is on.";} } public class JetOff extends Event{ public JetOff(long delayTime){super(delayTime);} public void action(){jet=false;} public String toString(){return "Jet is false.";} } } public class Ja10_8_25 { public static void main(String[] args) { GreenhouseControls2 gc = new GreenhouseControls2(); // Instead of hard-wiring, you could parse // configuration information from a text file here: gc.addEvent(gc.new Bell(900)); Event[] eventList = { gc.new ThermostatNight(0), gc.new LightOn(200), gc.new LightOff(400), gc.new WaterOn(600), gc.new WaterOff(800), gc.new ThermostatDay(0), gc.new FanOn(0), gc.new FanOff(0), gc.new JetOn(0), gc.new JetOff(0) }; gc.addEvent(gc.new Restart(2000, eventList)); gc.addEvent( new GreenhouseControls.Terminate( 2000)); gc.run(); } }

练习26:

/* Create a class with an inner class that has a non-default constructor * (one that takes arguments). Create a second class with an inner * class that inherits from the first inner class. */ import static net.mindview.util.Print.*; class A{ class inA{ inA(){print("A.inA()");} } } class B{ class inB extends A.inA{ inB(A a){a.super();} } } public class Ja10_8_26{ public static void main(String[] args){ A a=new A(); B b=new B(); B.inB inb=b.new inB(a); } }

转载请注明原文地址: https://www.6miu.com/read-2620347.html

最新回复(0)