1. 第三题

    package net.mindview.interfaces;
    abstract class Base{
    public Base(){
    print();
    }
    abstract void print();
    }
    public class Test3 extends Base{
    private int i = ;
    @Override
    void print() {
    System.out.println(i);
    }
    public static void main(String[] args) {
    Test3 t = new Test3();
    t.print();
    }
    }

    输出结果:

    
    

    调用基类构造方法的时候, 只是给子类的成员变量分配了一块内存空间, 并将内存空间的值设置为默认值0. 当真正调用子类构造方法之前才会为成员变量赋值.

  2. 第七题
    package net.mindview.interfaces;
    
    //啮(nie四声)齿动物
    interface Rodent{
    void say();
    } //老鼠
    class Mouse implements Rodent{
    public void say(){System.out.println("hi,我是 Mouse");}
    } //鼹鼠
    class Gerbil implements Rodent{
    public void say(){System.out.println("hi,我是 Gerbil");}
    } //大颊鼠
    class Hamster implements Rodent{
    public void say(){System.out.println("hi,我是 Hamster");}
    }
    public class RodentHome {
    public static void instroduce(Rodent rodent){
    rodent.say();
    } public static void instroduceAll(Rodent[] rodents){
    for(Rodent r: rodents){
    instroduce(r);
    }
    }
    public static void main(String[] args) {
    Rodent[] rodents = {
    new Mouse(),
    new Gerbil(),
    new Hamster()
    };
    instroduceAll(rodents);
    } }
  3. 第八题
    package net.mindview.interfaces;
    
    import java.util.Random;
    
    /** 定义一个乐器类 */
    interface Instrucment {
    int value = ; //定义在接口中的成员是static&final的
    void play(Note n);
    void adjust();
    } /**抽象类*/
    abstract class PublicMethod implements Instrucment{
    public abstract void play(Note n);
    //这个方法不用谢,以为继承自Object的类都有toString()方法
    //public abstract String toString();
    public abstract void adjust();
    } /**定义n个子类*/
    class Wind extends PublicMethod {
    public void play(Note n){ System.out.println("Wind.play() " + n);} public String toString(){ return "Wind.what()";} public void adjust(){ System.out.println("Wind.adjust()");}
    } class Purcussion extends PublicMethod{
    public void play(Note n){ System.out.println("Purcussion.play() " + n);} public String toString(){ return "Purcussion.what()";} public void adjust(){ System.out.println("Purcussion.adjust()");}
    } class Stringed extends PublicMethod{
    public void play(Note n){ System.out.println("Stringed.play() " + n);} public String toString(){ return "Stringed.what()";} public void adjust(){ System.out.println("Stringed.adjust()");}
    } class Brass extends Wind{
    public void play(Note n){ System.out.println("Brass.play() " + n);} public void adjust(){ System.out.println("Brass.adjust()");}
    } class WoodWind extends Wind{
    public void play(Note n){ System.out.println("WoodWind.play() " + n);} public String toString(){ return "WoodWind.what()";}
    } class Other extends Wind{
    public void play(Note n){ System.out.println("Other.play() " + n);} public String toString(){ return "Other.what()";}
    } /** 定义一个随机乐器生成器 */
    class RandomInstrucmentGenerator {
    Random rand = new Random();
    public Instrucment next(){
    switch(rand.nextInt()){
    default:
    case : return new Wind();
    case : return new Purcussion();
    case : return new Stringed();
    case : return new Brass();
    case : return new WoodWind();
    case : return new Other(); }
    }
    } public class Music5 { public static void tune(Instrucment i){
    i.play(Note.MIDDLE_C);
    i.toString();
    } public static void tuneAll(Instrucment[] e){
    for(Instrucment i : e){
    tune(i);
    }
    } private static RandomInstrucmentGenerator gen = new RandomInstrucmentGenerator();
    public static void main(String[] args) { /*Instrucment[] orchestra = {
    new Wind(),
    new Purcussion(),
    new Stringed(),
    new Brass(),
    new WoodWind(),
    new Other()
    };*/
    Instrucment[] ins = new Instrucment[];
    for(int i=; i<ins.length; i++){
    ins[i] = Music5.gen.next();
    } tuneAll(ins);
    }
    }
  4. 练习11--这个练习是巩固如何写适配器设计模式
    package net.mindview.interfaces;
    
    /**
    * 字符串反转类
    */
    public class StringReverse {
    public String name(){
    return getClass().getSimpleName();
    }
    //反转
    public String reverse(String s) {
    char[] array = s.toCharArray();
    String reverse = "";
    for (int i = array.length - ; i >= ; i--) {
    reverse += array[i];
    }
    return reverse;
    }
    }
    package net.mindview.interfaces;
    
    public class StringReverseAdapter implements Processor{
    StringReverse stringReverse;
    public StringReverseAdapter(StringReverse stringReverse){
    this.stringReverse = stringReverse;
    }
    @Override
    public String name() {
    // TODO Auto-generated method stub
    return stringReverse.name();
    } @Override
    public Object process(Object input) {
    // TODO Auto-generated method stub
    return stringReverse.reverse((String)input);
    }
    }

    在使用的时候,可以直接调用Apply的process方法

    public static void main(String[] args) {
    Apply.process(new StringReverseAdapter(new StringReverse()), "i am lily");
    }

    Apply方法没有写出来,这个类实在课文内部定义的,可以参考http://www.cnblogs.com/ITPower/p/8550627.html中第二点:解耦的案例一,案例二和案例三. 其中Apply类定义在案例一中。

  5. 第十二题
    package net.mindview.interfaces;
    
    interface CanFight {
    void fight();
    } interface CanSwim {
    void swim();
    } interface CanFly {
    void fly();
    } interface CanClimb {
    void climb();
    } //行为特征
    class ActionCharacter {
    public void fight(){ }
    } class Hero extends ActionCharacter implements CanFight,CanSwim,CanFly,CanClimb{
    @Override
    public void fly() { } @Override
    public void swim() { } @Override
    public void climb() { }
    } //冒险
    public class Adventure {
    public static void f(CanFly fly){
    fly.fly();
    }
    public static void s(CanSwim swim){
    swim.swim();
    }
    public static void v(CanFight fight){
    fight.fight();
    }
    public static void m(ActionCharacter ac){
    ac.fight();
    }
    public static void p(CanClimb c){
    c.climb();
    }
    public static void main(String[] args) {
    Hero hero = new Hero();
    f(hero);
    s(hero);
    v(hero);
    m(hero);
    p(hero); } }
  6. 第十四题:这道题的思想和书上p180页的案例思想一样.继承+多次实现接口
    package net.mindview.interfaces;
    
    interface BaseInterface1 {
    public void a();
    public void b();
    } interface BaseInterface2 {
    public void c();
    public void d();
    } interface BaseInterface3 {
    public void e();
    public void f();
    } interface Interface4 extends BaseInterface1,BaseInterface2,BaseInterface3{
    public void g(); } class Specific implements Interface4{
    public void h(){ } @Override
    public void a() { } @Override
    public void b() { } @Override
    public void c() { } @Override
    public void d() { } @Override
    public void e() { } @Override
    public void f() { } @Override
    public void g() { }
    } public class Test14 extends Specific implements Interface4{
    public static void aa(BaseInterface1 b1){
    b1.a();
    b1.b();
    } public static void bb(BaseInterface2 b){
    b.c();
    b.d();
    } public static void cc(BaseInterface3 b){
    b.e();
    b.f();
    } public static void dd(Interface4 b){
    b.g();
    }
    public static void main(String[] args) {
    Specific specific = new Specific();
    aa(specific);
    bb(specific);
    cc(specific);
    dd(specific);
    }
    }
  7. 第十六题
    package net.mindview.interfaces;
    
    import java.io.IOException;
    import java.nio.CharBuffer;
    import java.util.Random;
    import java.util.Scanner; class RandomChar {
    Random rand = new Random();
    Random count = new Random();
    private static final char[] captials = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
    public char[] make(){
    StringBuffer sb = new StringBuffer("");
    for(int i=; i<count.nextInt(); i++){
    sb.append(captials[rand.nextInt(captials.length)]);
    }
    return sb.toString().toCharArray();
    } public static void main(String[] args) {
    RandomChar rc = new RandomChar();
    char[] c = rc.make();
    System.out.println(c); for(char ch:c){
    System.out.print(ch);
    }
    }
    } public class AdapterRandomChar implements Readable{
    RandomChar rc;
    private int count;
    public AdapterRandomChar(RandomChar rc, int count){
    this.rc = rc;
    this.count = count;
    }
    @Override
    public int read(CharBuffer cb) throws IOException {
    if(count-- == ){
    return -;
    }
    StringBuffer sb = new StringBuffer("");
    for(char c:rc.make()){
    sb.append(c);
    }
    String result = sb.toString() + " " ;
    cb.append(result);
    return result.length();
    } public static void main(String[] args) {
    Scanner s = new Scanner(new AdapterRandomChar(new RandomChar(), ));
    while(s.hasNext()){
    System.out.print(s.next()+" ");
    }
    }
    }
  8. 第十八题
    package net.mindview.interfaces;
    //产品
    interface Cycle {
    } class Unicycle implements Cycle{
    public Unicycle(){
    System.out.println("我是一个Unicycle");
    }
    } class Bicycle implements Cycle{
    public Bicycle(){
    System.out.println("我是一个Bicycle");
    }
    } class Tricycle implements Cycle{
    public Tricycle(){
    System.out.println("我是一个Tricycle");
    }
    } //工厂类
    interface CycleFactory{
    public Cycle make();
    } class UnicycleFactory implements CycleFactory{
    @Override
    public Cycle make() {
    return new Unicycle();
    } } class BicycleFactory implements CycleFactory{
    @Override
    public Cycle make() {
    return new Bicycle();
    } } class TricycleFactory implements CycleFactory{
    @Override
    public Cycle make() {
    return new Tricycle();
    } } public class CycleCustomer {
    public static Cycle serviceCustoemr(CycleFactory fact){
    return fact.make();
    }
    public static void main(String[] args) {
    Cycle u = serviceCustoemr(new UnicycleFactory());
    Cycle b = serviceCustoemr(new BicycleFactory());
    Cycle t = serviceCustoemr(new TricycleFactory()); } }
  9. 第十九题
    package net.mindview.interfaces;
    
    import java.util.Random;
    
    /**
    * 这时一个抛硬币和掷骰子等类型的框架
    */ interface ThrowProduct {} class ThrowCorn implements ThrowProduct{
    Random rand = new Random();
    public ThrowCorn(){
    if(rand.nextInt() % ==){
    System.out.println("硬币的正面");
    }else{
    System.out.println("硬币的反面");
    }
    }
    } class ThrowDice implements ThrowProduct{
    Random rand = new Random();
    public ThrowDice(){
    System.out.println("掷的骰子数是"+rand.nextInt());
    }
    } interface ThrowFactory{
    ThrowProduct throwOut();
    } class ThrowCornFactory implements ThrowFactory{
    public ThrowCornFactory(){
    System.out.print("开始抛硬币:");
    }
    @Override
    public ThrowProduct throwOut() {
    return new ThrowCorn();
    }
    } class ThrowDiceFactory implements ThrowFactory{
    public ThrowDiceFactory(){
    System.out.print("开始掷骰子:");
    }
    @Override
    public ThrowProduct throwOut() {
    return new ThrowDice();
    }
    } public class ThrowFrame {
    public static ThrowProduct service(ThrowFactory f){
    return f.throwOut();
    }
    public static void main(String[] args) {
    service(new ThrowCornFactory());
    service(new ThrowDiceFactory()); } }

    结果:

    开始抛硬币:硬币的正面
    开始掷骰子:掷的骰子数是6

java编程思想第四版第九章习题的更多相关文章

  1. java编程思想第四版第九章总结

    1. 策略设计模式 参考这篇文章:http://blog.csdn.net/chenjie19891104/article/details/6396458 讲的很清楚,策略设计模式.并且举了一个例子, ...

  2. Java编程思想第四版第二章练习题答案

    练习1:创建一个类,它包含一个int域和一个char域,它们都没有被初始化.将他们的值打印出来,以验证Java执行了默认初始化 public class JavaThinking { private ...

  3. java编程思想第四版第二章要点总结

    1. 基本类型 基本类型 二进制位数 包装器类 boolean - Boolean byte 8 Byte char 16 Character short 16 Short int 32 Intege ...

  4. java编程思想第四版第十章习题

    第一题 package net.mindview.innerclasses; public class Outer { class Inner { Inner(){ System.out.printl ...

  5. java编程思想第四版第八章习题

    第一题 package net.mindview.polymorphism; //基类-自行车 class Cycle{ } //子类-单轮车 class Unicycle extends Cycle ...

  6. 《Java编程思想第四版》附录 B 对比 C++和 Java

    <Java编程思想第四版完整中文高清版.pdf>-笔记 附录 B 对比 C++和 Java “作为一名 C++程序员,我们早已掌握了面向对象程序设计的基本概念,而且 Java 的语法无疑是 ...

  7. java编程思想第四版中net.mindview.util包下载,及源码简单导入使用

    在java编程思想第四版中需要使用net.mindview.util包,大家可以直接到http://www.mindviewinc.com/TIJ4/CodeInstructions.html 去下载 ...

  8. Java编程思想第四版勘误

    坊间传说这本书翻译得很烂,我倒觉得还好.虽然看原文更准确,但是如果在具备一定编程思维和基础.能够看出来疑问的情况下,还是看中文更快一些,而且这本书本身也不适合初学者看.当然,错误和不通顺还是有的,而且 ...

  9. Java编程思想第四版完整中文高清版.pdf

    Java编程思想第四版完整中文高清版.pdf 链接: https://pan.baidu.com/s/1vV5BHF3L-bnaG6WGurdJ_A 提取码: vigy 复制这段内容后打开百度网盘手机 ...

随机推荐

  1. Web渗透之mssql差异备份getshell

    简介 差异备份数据库得到webshell.在sql server 里dbo和sa权限都有备份数据库权限,我们可以把数据库备份称asp文件,这样我们就可以通过mssqlserver的备份数据库功能生成一 ...

  2. php代码Xdebug调试使用笔记

    0x01 Xdebug简介 Xdebug是一个开放源代码的PHP程序调试器 运行流程: 0x02  Xdebug配置 日志 xdebug.trace_output_dir: 日志追踪输出目录 xdeb ...

  3. Python3的编码整理总结

    python3在内存中是用unicode编码方式存储的,所以不能直接储存和传输,要转化为其他编码进行储存和传输. 字符串通过编码转换成字节码,字节码通过解码成为字符串 encode:str --> ...

  4. python使用input().split()接收多个用户输入

    1.input() 接收多个用户输入需要与split()结合使用 host, port, username, passwd, dbname = input("请输入服务器地址,端口号,用户名 ...

  5. canvas模拟中国铁路运行图

    原理说明 1.在知道canvas画布尺寸的情况下,需要将地理经纬度信息转换为canvas画布x,y坐标,因为中国地图地理经纬度坐标取值范围为73.33-135.05(经度)37-50(维度),所以第一 ...

  6. 从零开始把项目发布到maven仓库中心

    sonatype准备操作 注册账号 https://issues.sonatype.org 1. 密码符号规范,并且工记住 新建项目 1. group id 如果你有com域名的所有权可以直接使用,如 ...

  7. CSS 阴影动画优化技巧一则

    本技巧来自这篇文章 -- How to animate box-shadow with silky smooth performance 本文不是直译,因为觉得这个技巧很有意思很有用,遂起一文. bo ...

  8. 小白学 Python(13):基础数据结构(字典)(下)

    人生苦短,我选Python 前文传送门 小白学 Python(1):开篇 小白学 Python(2):基础数据类型(上) 小白学 Python(3):基础数据类型(下) 小白学 Python(4):变 ...

  9. Java基础(三十二)JDBC(2)连接数据库

    一.连接数据库的过程 连接数据库的过程:加载数据库驱动程序,不过只需在第一次访问数据库时加载一次,然后在每次访问数据库时创建一个Connection实例,然后执行操作数据库的SQL语句,并返回执行结果 ...

  10. 【原创】go语言学习(十一)package简介

    目录 Go源码组织方式 main函数和main包 编译命令 自定义包 init函数以及执行行顺序 _标识符 Go源码组织方式 1. Go通过package的方式来组织源码 package 包名 注意: ...