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());
}
}
总结:
这一章,两个设计模式暂时不是很懂可以做什么,但是了解了接口的大概使用方法。