专栏首页盛开在夏天的太阳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();
    }
}
  1. 第二题
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();
        }
    }

}
  1. 第三题
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());
        }
    }

}
  1. 第四题
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();
        }
    }

}
  1. 第五题
package net.mindview.innerclasses;

class Outter{
    class Inner{
        
    }
}

public class Test5 {
    public void getInner(){
        Outter outter = new Outter();
        Outter.Inner in = outter.new Inner();
    }
}
  1. 第六题
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.one;

public interface One {
    public String one();
}
  1. 第七题
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();
    }
}
  1. (略)
  2. 第九题
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());
    }
}
  1. 第十题
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));
    }
}
  1. 第十一题
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();
    }

}
  1. 第十二题
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();
    }
}
  1. 第十三题
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));
    }
}
  1. (略)
  2. 第十五题
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());
    }

}
  1. 第十六题
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);
    }

}
  1. 第十七题
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);

    }

}
  1. 第十八题
package net.mindview.innerclasses.test18;

public class Test18 {
    public static class QianTao{
        
    }
    public static void main(String[] args) {
        QianTao q = new QianTao();
    }
}
  1. 第十九题 编译后的文件结构:
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);
    }
}
  1. 第二十题
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();
    }
}
  1. 第二一题
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()");    
    }

}
  1. 第二十二题
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();
        }
        
        
    }

}
  1. 第二十三题
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 interface U {
    void method1();
    void method2();
    void method3();
}
  1. f
  2. f
  3. f
  4. f

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • java编程思想第四版第九章习题

    用户7798898
  • java编程思想第四版第十一章习题

    用户7798898
  • java编程思想第四版第七章习题

    用户7798898
  • JAVA中的23种设计模式(GOF)

    volatile 保证数据的可见性,让线程内存数据的变化立刻显示到主存中,而且有序性可以避免指令重排,但是不保证原子性。

    HcodeBlogger
  • 设计模式整理 顶

    Iterator模式可以帮助我们分离具体的集合跟遍历,就是在代码中更换了集合,也可以不需要重新调用新集合的方法。

    算法之名
  • 10(01)总结形式参数,包,修饰符,内部类

    类,抽象类,接口的综合小练习 /* 教练和运动员案例(学生分析然后讲解) 乒乓球运动员和篮球运动员。 乒乓球教练和篮球教练。 为了出国交流,跟乒乓球相关...

    Java帮帮
  • 超详细:常用的设计模式汇总

    简单点说,就是一个应用程序中,某个类的实例对象只有一个,你没有办法去new,因为构造器是被private修饰的,一般通过getInstance()的方法来获取它...

    Java团长
  • 来自 BAT 大牛总结的常用设计模式汇总

    原文链接:https://cnblogs.com/chenshuyong/p/9998164.html

    业余草
  • java编程思想第四版第九章习题

    用户7798898
  • 平时常用的设计模式汇总,超详细!【含福利】

    端午三天假期刚过,今天就跟大家重新复习一下编码开发中几种常见的设计模式 。本文主要以Java代码为例,分别跟大家介绍单例模式、观察者模式、装饰者模式、工厂模式、...

    挨踢小子部落阁

扫码关注云+社区

领取腾讯云代金券