Thinking in java Chapter9

Exercise1-

exercise1:

package Chapter9;

public class RodentTest {

    public static void main(String[] args) {
        Rodent[] rodent =new Rodent[2];
        rodent[0]= new Hamster();
        rodent[1]= new Mouse();
        for(int i=0;i<2;i++) {
            rodent[i].Bite();
        }
    }

}
abstract class Rodent{
    abstract void Bite(); 

    abstract String what();
}

class Mouse extends Rodent{
    void Bite() {
        System.out.println("Mouse Bite");
    }
    String what() {
        return "Mouse";
    }
}

class Hamster extends Rodent{
    void Bite() {
        System.out.println("Hamster Bite");
    }
    String what() {
        return "Hamster";
    }
}










output:
Hamster Bite
Mouse Bite

exercise2

exercise3:

package Chapter9;

public class Exercise3 {

    public static void main(String[] args) {
        Test t = new Test();
        t.print();
    }

}

abstract class Print{
    Print() {
        print();
    }
    abstract void print();
}

class Test extends Print{
    private int i=3;
    @Override
    void print() {
        System.out.println(i);
    }

}
output:
0
3

程序从基类的构造器开始执行,进入基类的构造器后执行导出类的覆盖方法,但是此时i并没有被初始化,因此输出为0,然后是开始导出类的初始化。

exercise4:

没懂。。。。

exercise11:

package Chapter9.Interface;

public interface Processor {
    String name();
    Object process(Object input);
}

package Chapter9.Interface;


public class Apply {
    public static void process(Processor p,Object s) {
        System.out.println("Using Processor "+p.name());
        System.out.println(p.process(s));
    }
}
package Chapter9;


import java.util.Scanner;

public class StringChange {
    public String name() {
        return getClass().getSimpleName();
    }


    public  String change(String s) {
        String[] change = s.split(" ");
        int j = change.length;
        for(int i=0;i<(j/2);i++) {
            String temp=change[i];
            change[i] = change[change.length-i-1];
            change[change.length-i-1] = temp;
        }
        String s1 = "";
        for(int i=0;i<change.length;i++) {
            s1 = s1 +" "+change[i];
        }
        return s1;
    }

    public static void main(String[] args) {
        String s = "I love apple.";
        StringChange sc = new StringChange();
        System.out.println(sc.change(s));
    }
}
package Chapter9;

import Chapter9.Interface.Apply;
import Chapter9.Interface.Processor;

class StringChangeAdapter implements Processor{
    StringChange sc;

    public StringChangeAdapter(StringChange sc) {
        this.sc = sc;
    }
    @Override
    public String name() {
        return sc.name();
    }

    @Override
    public Object process(Object input) {
        return sc.change((String)input);
    }

}
public class StringChangeProcessor {

    public static void main(String[] args) {
        String s = "I love apple.";
        Apply.process(new StringChangeAdapter(new StringChange()), s);
    }

}

这个设计模式,, 没懂。。。建议在看看其他博客。

exercise17:

package Chapter9;


interface ceshi{
    int i=10;
    int j=20;
}
public class Exercise17 {

    public static void main(String[] args) {
        new  a();
        new b();

    }

}

class a implements ceshi{
    a(){
        //a.i++; //i不可更改
        System.out.println(a.i);
    }
}
class b implements ceshi{
    b(){
        //a.i++;
        System.out.println(b.i);
    }
}

exercise18:

package Chapter9;


interface Cycle{
    void move();
}
interface CycleFactory{
    Cycle getCycle();
}

class Unicycle implements Cycle{

    public void move() {
        System.out.println("Unicycle move!");
    }

}
class UnicycleFactory implements CycleFactory{


    public Cycle getCycle() {
        return new Unicycle();
    }

}



class Tricycle implements Cycle{

    public void move() {
        System.out.println("Tricycle move!");
    }

}
class TricycleFactory implements CycleFactory{


    public Cycle getCycle() {
        return new Tricycle();
    }

}



class Bicycle implements Cycle{

    public void move() {
        System.out.println("Bicycle move!");
    }

}

class BicycleFactory implements CycleFactory{


    public Cycle getCycle() {
        return new Bicycle();
    }

}


public class Cycles {
    public static void moveCycle(CycleFactory cf) {
        Cycle c = cf.getCycle();
        c.move();
    }

    public static void main(String[] args) {
        moveCycle(new BicycleFactory());
        moveCycle(new TricycleFactory());
        moveCycle(new UnicycleFactory());
    }

}

exercise19:

package Chapter9;

import java.util.Random;

interface CoinAndDice{
    void Throw();
}
interface CoinAndDiceFactory{
    CoinAndDice getCoinAndDice();
}

class Coin implements CoinAndDice{
    Random rand = new Random();
    public void Throw() {
        if(rand.nextInt(2)==0)
            System.out.println("Coin:"+"head");
        else 
            System.out.println("Coin:"+"tail");
    }

}
class CoinFactory implements CoinAndDiceFactory{

    public CoinAndDice getCoinAndDice() {
        return new Coin();
    }

}

class Dice implements CoinAndDice{
    Random rand = new Random();
    public void Throw() {
        System.out.println("Dice:"+(rand.nextInt(6)+1));
    }

}
class DiceFactory implements CoinAndDiceFactory{

    public CoinAndDice getCoinAndDice() {
        return new Dice();
    }

}


public class CoinsAndDices {
    public static void Throws(CoinAndDiceFactory cdf) {
        CoinAndDice cd =cdf.getCoinAndDice();
        cd.Throw();
    }
    public static void main(String[] args) {
        Throws(new CoinFactory());
        Throws(new CoinFactory());
        Throws(new CoinFactory());
        Throws(new DiceFactory());
        Throws(new DiceFactory());
        Throws(new DiceFactory());
    }

}

总结:

这一章,两个设计模式暂时不是很懂可以做什么,但是了解了接口的大概使用方法。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值