zoukankan      html  css  js  c++  java
  • java编程思想第四版第十章习题

    1. 第一题
      package net.mindview.innerclasses;
      
      public class Outer {
          class Inner {
              Inner(){
                  System.out.println("这是一个内部类");
              }
          }
      
          public Inner in(){
              return new Inner();
          }
          
          public static void main(String[] args) {
              Outer out = new Outer();
              Outer.Inner inner = out.in();
          }
      }
    2. 第二题
      package net.mindview.innerclasses;
      /**
       * 选择器
       */
      interface Selector3 {
          //是否结束
          boolean end();
          //当前
          Object current();
          //下一个
          void next();
      }
      
      class OtherObject3 {
          String obj;
          OtherObject3(String obj){
              this.obj = obj;
          }
          
          @Override
          public String toString() {
              return obj;
          }
      }
      
      /**
       * 次序
       */
      public class Sequence3 {
          private Object[] items;
          private int next = 0;
          //定义数组的个数
          public Sequence3(int size){
              items = new Object[size];
          }
          //添加
          public void add(Object x){
              items[next ++] = x;
          }
          
          //内部类--顺序选择器
          private class SequenceSelector implements Selector {
              //选择器最开始指向第一个元素
              private int i = 0;
              @Override
              public boolean end() {
                  return i == (items.length);
              }
      
              @Override
              public Object current() {
                  // TODO Auto-generated method stub
                  return items[i];
              }
      
              @Override 
              public void next() {
                  if(i<items.length){
                      i++;
                  }
              }
          }
          
          public Selector selector() {
              return new SequenceSelector();
          }
          
          public static void main(String[] args) {
              Sequence3 s = new Sequence3(10);
              for(int i=0; i<10; i++){
                  s.add(new OtherObject("对象"+i));
              }
              Selector selector = s.selector();
              while(!selector.end()){
                  System.out.println(selector.current());
                  selector.next();
              }
          }
      
      }
    3. 第三题
      package net.mindview.innerclasses;
      /**
       * 选择器
       */
      interface Selector3 {
          //是否结束
          boolean end();
          //当前
          Object current();
          //下一个
          void next();
          @Override
          public String toString();
      }
      
      class OtherObject3 {
          String obj;
          OtherObject3(String obj){
              this.obj = obj;
          }
          
          @Override
          public String toString() {
              return obj;
          }
      }
      
      /**
       * 次序
       */
      public class Sequence3 {
          private Object[] items;
          private int next = 0;
          private String str;
          //定义数组的个数
          public Sequence3(int size, String str){
              items = new Object[size];
              this.str = str;
          }
          //添加
          public void add(Object x){
              items[next ++] = x;
          }
          
          //内部类--顺序选择器
          private class SequenceSelector implements Selector3 {
              //选择器最开始指向第一个元素
              private int i = 0;
              @Override
              public boolean end() {
                  return i == (items.length);
              }
      
              @Override
              public Object current() {
                  // TODO Auto-generated method stub
                  return items[i];
              }
      
              @Override 
              public void next() {
                  if(i<items.length){
                      i++;
                  }
              }
              
              public String toString() {
                  return str;
              }
          }
          
          public Selector3 selector() {
              return new SequenceSelector();
          }
          
          public static void main(String[] args) {
              Sequence3 s = new Sequence3(10, "三星");
              for(int i=0; i<10; i++){
                  s.add(new OtherObject("对象"+i));
              }
              Selector3 selector = s.selector();
              while(!selector.end()){
                  System.out.println(selector.current());
                  selector.next();
                  System.out.println(selector.toString());
              }
          }
      
      }
    4. 第四题
      package net.mindview.innerclasses;
      /**
       * 选择器
       */
      interface Selector {
          //是否结束
          boolean end();
          //当前
          Object current();
          //下一个
          void next();
      }
      
      class OtherObject {
          String obj;
          OtherObject(String obj){
              this.obj = obj;
          }
          
          @Override
          public String toString() {
              return obj;
          }
      }
      
      /**
       * 次序
       */
      public class Sequence {
          private Object[] items;
          private int next = 0;
          //定义数组的个数
          public Sequence(int size){
              items = new Object[size];
          }
          //添加
          public void add(Object x){
              items[next ++] = x;
          }
          
          //内部类--顺序选择器
          private class SequenceSelector implements Selector {
              //选择器最开始指向第一个元素
              private int i = 0;
              @Override
              public boolean end() {
                  return i == (items.length);
              }
              /**
               * 新增方法: 在内部类中引用外部类
               */
              public Sequence outer(){
                  return Sequence.this;
              }
      
              @Override
              public Object current() {
                  // TODO Auto-generated method stub
                  return items[i];
              }
      
              @Override 
              public void next() {
                  if(i<items.length){
                      i++;
                  }
              }
          }
          
          public Selector selector() {
              return new SequenceSelector();
          }
          
          public static void main(String[] args) {
              Sequence s = new Sequence(10);
              for(int i=0; i<10; i++){
                  s.add("对象"+i);
              }
              Selector selector = s.selector();
              while(!selector.end()){
                  System.out.println(selector.current());
                  selector.next();
              }
          }
      
      }
    5. 第五题
      package net.mindview.innerclasses;
      
      class Outter{
          class Inner{
              
          }
      }
      
      public class Test5 {
          public void getInner(){
              Outter outter = new Outter();
              Outter.Inner in = outter.new Inner();
          }
      }
    6. 第六题
      package net.mindview.innerclasses.test6.one;
      
      public interface One {
          public String one();
      }
      package net.mindview.innerclasses.test6.two;
      
      import net.mindview.innerclasses.test6.one.One;
      
      public class Two {
          protected class TwoInner implements One{
              public TwoInner() {
                  System.out.println("这时two的内部类,继承自One");
              }
      
              @Override
              public String one() {
                  return "Two->one()";
              }
              
          }
      }
      package net.mindview.innerclasses.test6.three;
      
      import net.mindview.innerclasses.test6.one.One;
      import net.mindview.innerclasses.test6.two.Two;
      
      public class Three extends Two{
          private Two two;
          public Three(Two two){
              System.out.println("这时Three类");
              this.two = two;
          }
          public One three(){
              
              return two.new TwoInner();
          }
          public static void main(String[] args) {
              Three three = new Three(new Two());
              One one = three.three();
              System.out.println(one.one());
          }
      
      }
    7. 第七题
      package net.mindview.innerclasses.test7;
      interface Update{
          String update();
      }
      public class Show {
          private String str;
          
          private String showStr(){
              return str;
          }
          
          private class InnerUpdate implements Update{
              private int abc = 123;
              public String update(){
                  str = "内部类修改";
                  System.out.println(str);
                  return showStr();
              }
          }
          
          public void visitInner(){
              InnerUpdate update = new InnerUpdate();
              System.out.println(update.abc);
              update.update();
          }
          
          public static void main(String[] args) {
              Show show = new Show();
              show.visitInner();
          }
      }
    8. (略)
    9. 第九题
      package net.mindview.innerclasses.test9;
      
      interface Show{
          String show();
      }
      
      public class Test9 {
          public String update(){
              class Inner implements Show{
                  @Override
                  public String show() {
                      return "学习";
                  }
              }
              
              Inner inner = new Inner();
              return inner.show();
          }
          public static void main(String[] args) {
              Test9 t = new Test9();
              System.out.println(t.update());
          }
      }
    10. 第十题
      package net.mindview.innerclasses.test9;
      
      interface Show{
          String show();
      }
      
      public class Test9 {
          public String update(boolean flag){
              String str=null;
              if(flag){
                  class Inner implements Show{
                      @Override
                      public String show() {
                          return "学习";
                      }
                  }
                  Inner inner = new Inner();
                  str = inner.show();
              }
              return str;
          }
          public static void main(String[] args) {
              Test9 t = new Test9();
              System.out.println(t.update(true));
          }
      }
    11. 第十一题
      package net.mindview.innerclasses.test10;
      
      interface InterfaceClass{
          void i();
      }
      
      public class Test10 {
          private class Inner implements InterfaceClass{
              @Override
              public void i() {
              }
          }
          
          public InterfaceClass inner(){
              return new Inner();
          }
          
          public static void main(String[] args) {
              Test10 test10 = new Test10();
              InterfaceClass inner = test10.inner();
              //这样不可以,所以被隐藏了
              //Inner inner = test10.inner();
          }
      
      }
    12. 第十二题
      package net.mindview.innerclasses.test12;
      interface Update{
          String update();
      }
      public class Show {
          private String str;
          
          private String showStr(){
              return str;
          }
          
          private class InnerUpdate implements Update{
              private int abc = 123;
              public String update(){
                  str = "内部类修改";
                  System.out.println(str);
                  return showStr();
              }
          }
          
          public Update visitInner(){
              return new Update(){
                  private int abc = 123;
                  @Override
                  public String update() {
                      str = "内部类修改";
                      System.out.println(str);
                      return showStr();
                  }
              };
          }
          
          public static void main(String[] args) {
              Show show = new Show();
              show.visitInner().update();
          }
      }
    13. 第十三题
      package net.mindview.innerclasses.test13;
      
      interface Show{
          String show();
      }
      
      public class Test13 {
          public String update(boolean flag){
              String str=null;
              if(flag){
                  return new Show(){
                      @Override
                      public String show() {
                          return "学习";
                      }
                      
                  }.show();
              }
              return str;
          }
          public static void main(String[] args) {
              Test13 t = new Test13();
              System.out.println(t.update(true));
          }
      }
    14. (略)
    15. 第十五题
      package net.mindview.innerclasses.test15;
      
      class Person {
          private String name;
          public Person(String name){
              this.name = name;
          }
          @Override
          public String toString() {
              return name;
          }
      }
      public class Test15 {
          public Person method(String name){
              return new Person(name){
                  @Override
                  public String toString() {
                      return super.toString();
                  }
              };
          }
          public static void main(String[] args) {
              Test15 t = new Test15();
              System.out.println(t.method("张三").toString());
          }
      
      }
    16. 第十六题
      package net.mindview.innerclasses.test16;
      interface Cycle {
          void make();
      }
      
      interface CycleFactory {
          Cycle getCycle();
      }
      
      class Unicycle implements Cycle{
          private Unicycle(){}
      
          @Override
          public void make() {
              System.out.println("make Unicycle");
          }
          
          public static CycleFactory factory = new CycleFactory(){
              @Override
              public Cycle getCycle() {
                  return new Unicycle();
              }
          };
      }
      
      class Bicycle implements Cycle{
          private Bicycle(){}
      
          @Override
          public void make() {
              System.out.println("make Bicycle");
          }
          
          public static CycleFactory factory = new CycleFactory(){
              @Override
              public Cycle getCycle() {
                  return new Bicycle();
              }
          };
      }
      
      class Tricycle implements Cycle{
          private Tricycle(){}
      
          @Override
          public void make() {
              System.out.println("make Tricycle");
          }
          
          public static CycleFactory factory = new CycleFactory(){
              @Override
              public Cycle getCycle() {
                  return new Tricycle();
              }
          };
      }
      
      public class Cycles {
          public static void serice(CycleFactory factory){
              Cycle c = factory.getCycle();
              c.make();
          }
          public static void main(String[] args) {
              //我想要什么样的车,就找这种车的工厂去制作一辆车就可以了
              serice(Unicycle.factory);
              serice(Bicycle.factory);
              serice(Tricycle.factory);
          }
      
      }
    17. 第十七题
      package net.mindview.innerclasses.test17;
      
      import java.util.Random;
      
      /**
       * 这时一个抛硬币和掷骰子等类型的框架
       */
      
      interface ThrowProduct {}
      interface ThrowFactory{
          ThrowProduct getProduct();
      }
      class ThrowCorn implements ThrowProduct{
          Random rand = new Random();
          private ThrowCorn(){
              if(rand.nextInt(100) % 2 ==0){
                  System.out.println("硬币的正面");
              }else{
                  System.out.println("硬币的反面");
              }
          }
          
          public static ThrowFactory factory = new ThrowFactory(){
              @Override
              public ThrowProduct getProduct() {
                  return new ThrowCorn();
              }
          };
      }
      
      class ThrowDice implements ThrowProduct{
          Random rand = new Random();
          private ThrowDice(){
              System.out.println("掷的骰子数是"+(rand.nextInt(6)+1));
          }
          
          public static ThrowFactory factory = new ThrowFactory(){
              @Override
              public ThrowProduct getProduct() {
                  return new ThrowDice();
              }
          };
      }
      
      public class ThrowFrame {
          public static void service(ThrowFactory f){
              ThrowProduct p = f.getProduct();
          }
          public static void main(String[] args) {
              service(ThrowCorn.factory);
              service(ThrowDice.factory);
      
          }
      
      }
    18. 第十八题
      package net.mindview.innerclasses.test18;
      
      public class Test18 {
          public static class QianTao{
              
          }
          public static void main(String[] args) {
              QianTao q = new QianTao();
          }
      }
    19. 第十九题
      package net.mindview.innerclasses.test19;
      
      public class Test19 {
          //这时一个嵌套类
          public static class Inner1{
              
              //这时定义在嵌套类中的嵌套类
              static class Inner2{
                  public static int i = 100; 
              }
          }
          public static void main(String[] args) {
              System.out.println(Inner1.Inner2.i);
          }
      }

      编译后的文件结构:

    20. 第二十题
      package net.mindview.innerclasses.test20;
      interface InnerInInterface{
          static class Inner{
              public Inner(){
                  System.out.println("嵌套类构造方法");
              }
          }
      }
      
      public class Test20 {
          public static void main(String[] args) {
              //直接使用  new 外围类.嵌套类
              InnerInInterface.Inner inner = new InnerInInterface.Inner();
          }
      }
    21. 第二一题
      package net.mindview.innerclasses.test21;
      interface InterfaceClass {
          void f();
          void b();
          class InnerClass{
              static void get(InterfaceClass impl){
                  impl.f();
              }
          }
      }
      public class Test21 implements InterfaceClass{
          public Test21(){
              System.out.println("这是Test21构造函数的方法");
          }
          public static void main(String[] args) {
              
              Test21 t = new Test21();
              new InterfaceClass.InnerClass().get(t);;
              
          }
      
          @Override
          public void f() {
              System.out.println("实现类   f()");
          }
      
          @Override
          public void b() {
              System.out.println("实现类   b()");    
          }
      
      }
    22. 第二十二题
      package net.mindview.innerclasses.test22;
      
      //选择器
      interface Selecotr {
          //选择器是否到达尾部
          boolean end();
          //下一个元素编号
          void next();
          //当前元素
          Object current();
          
      }
      
      public class Sequence {
          private Object[] items ;
          static int counter = 0;
          public Sequence(int size){
              items = new Object[size];
              for(int i=0; i<size; i++){
                  add("对象"+i);
              }
          }
          
          public void add(Object o){
              items[counter++] = o;
          }
          
          public  Selecotr sequenceSelector = new Selecotr(){
              int i = 0;
              @Override
              public boolean end() {
                  // TODO Auto-generated method stub
                  return i == items.length;
              }
              @Override
              public void next() {
                  if(i<items.length)
                      i++;
              }
              @Override
              public Object current() {
                  if(i<items.length){
                      return items[i];
                  }
                  return null;
              }
          };
          //反序
          public Selecotr reverseSelector = new Selecotr(){
              int i = 9;
              
              @Override
              public boolean end() {
                  return i<0;
              }
      
              @Override
              public void next() {
                  i--;
              }
      
              @Override
              public Object current() {
                  if(i>=0){
                      return items[i];
                  }
                  return null;
              }
              
          };
          
          public static void main(String[] args) {
              Sequence s = new Sequence(10);
              
              //正序
              Selecotr se = s.sequenceSelector;
              while(!se.end()){
                  System.out.println(se.current());
                  se.next();
              }
              System.out.println("-----------");
              //反序
              Selecotr re = s.reverseSelector;
              while(!re.end()){
                  System.out.println(re.current());
                  re.next();
              }
              
              
          }
      
      }
    23. 第二十三题
      package net.mindview.innerclasses.test23;
      
      public interface U {
          void method1();
          void method2();
          void method3();
      }
      package net.mindview.innerclasses.test23;
      
      public class A {
          U methodA1(){
              return new U(){
                  @Override
                  public void method1() {
                      System.out.println("A methodA1 method1()");
                  }
      
                  @Override
                  public void method2() {
                      System.out.println("A methodA1 method2()");
                  }
      
                  @Override
                  public void method3() {
                      System.out.println("A methodA1 method3()");
                  }
              };
          }
      }
      package net.mindview.innerclasses.test23;
      
      public class B {
          U[] us ;
          private static int counter = 0; 
          public B(int size){
              us = new U[size];
          }
          void methodB1(U u){
              us[counter++] = u;
          }
          void methodB2(int i){
              us[i] = null;
          }
          void methodB3(){
              for(int i=0; i<us.length; i++){
                  U u = us[i];
                  u.method1();
                  u.method2();
                  u.method3();
              }
          }
          
          public static void main(String[] args) {
              A a1 = new A();
              A a2 = new A();
              A a3 = new A();
              A a4 = new A();
              A a5 = new A();
              B b1 = new B(5);
              b1.methodB1(a1.methodA1());
              b1.methodB1(a2.methodA1());
              b1.methodB1(a3.methodA1());
              b1.methodB1(a4.methodA1());
              b1.methodB1(a5.methodA1());
              
              b1.methodB3();
              System.out.println("----------------");
              b1.methodB2(2);
              b1.methodB3();
          }
      }
    24. f
    25. f
    26. f
    27. f
  • 相关阅读:
    2019-9-2-正则表达式30分钟入门教程
    2019-6-23-开源项目使用-appveyor-自动构建
    2019-8-29-dotnet-core-使用-sqlite-部署到-Centos-服务器
    2018-10-19-Roslyn-使用-Directory.Build.props-文件定义编译
    2019-4-29-dotnet-通过-WMI-获取系统安装软件
    2018-12-24-win10-uwp-求两个矩形相连的几何
    shell公共函数functions
    linux防火墙和SELinux
    ubuntu开启ssh
    文件夹操作
  • 原文地址:https://www.cnblogs.com/ITPower/p/8567233.html
Copyright © 2011-2022 走看看