Java编程思想第八章答案(1)

1、

package ch8;
class Cycle{
	
}
class Unicycle extends Cycle{
	
}
class Bicycle extends Cycle{
	
}
class Tricycle extends Cycle{
	
}
public class UseCycle {
static void ride(Cycle c) {
	System.out.println("i'm using Cycle!");
}
public static void main(String[] args) {
	Unicycle uc=new Unicycle();
	Bicycle bc=new Bicycle();
	Tricycle tc=new Tricycle();
	ride(uc);
	ride(bc);
	ride(tc);
}
}

2、

package ch8;
import java.util.*;
class Shape{
	public void draw() {}
	public void erase() {}
}
class Circle extends Shape{
	@Override public void draw() {System.out.println("draw Circle");}
	@Override public void erase() {System.out.println("erase Circle");}
}
class Square extends Shape{
	@Override public void draw() {System.out.println("draw Square");}
	@Override public void erase() {System.out.println("erase Square");}
}
class Triangle extends Shape{
	@Override public void draw() {System.out.println("draw Triangle");}
	@Override public void erase() {System.out.println("erase Triangle");}
}
class RandomShapeGenerator{
	private Random rand=new Random(47);
	public Shape next() {
		switch(rand.nextInt(3)) {
		default:
		case 0:return new Circle();
		case 1:return new Square();
		case 2:return new Triangle();
		}
	}
}
public class Shapes {
private static RandomShapeGenerator gen=new RandomShapeGenerator();
public static void main(String[] args) {
	Shape[] s=new Shape[9];
	for(int i=0;i<s.length;i++)
		s[i]=gen.next();
	for(Shape shp:s)
		shp.draw();
}
}

3、不在构造器中添加,什么都不会发生。
4、一样
5、

package ch8;
class Cycle{
//int i;
int wheels() {
	return 0;
}
}
class Unicycle extends Cycle{
	//int i=1;
	int wheels() {
		return 1;
	}
}
class Bicycle extends Cycle{
	//int i=2;
	int wheels() {
		return 2;
	}
}
class Tricycle extends Cycle{
	//int i=3;
	int wheels() {
		return 3;
	}
}
public class UseCycle {
static void ride(Cycle c) {
	System.out.println(c.wheels());
}
public static void main(String[] args) {
	Unicycle uc=new Unicycle();
	Bicycle bc=new Bicycle();
	Tricycle tc=new Tricycle();
    ride(uc);
    ride(bc);
    ride(tc);
}
}

6、

//: polymorphism/music3/Music3.java
// An extensible program.
package polymorphism.music3;
import polymorphism.music.Note;
import static net.mindview.util.Print.*;

class Instrument {
  void play(Note n) { print("Instrument.play() " + n); }
  public String toString() { return "Instrument"; }
  void adjust() { print("Adjusting Instrument"); }
}

class Wind extends Instrument {
  void play(Note n) { print("Wind.play() " + n); }
  public String toString() { return "Wind"; }
  void adjust() { print("Adjusting Wind"); }
}	

class Percussion extends Instrument {
  void play(Note n) { print("Percussion.play() " + n); }
  public String toString() { return "Percussion"; }
  void adjust() { print("Adjusting Percussion"); }
}

class Stringed extends Instrument {
  void play(Note n) { print("Stringed.play() " + n); }
  public String toString() { return "Stringed"; }
  void adjust() { print("Adjusting Stringed"); }
}

class Brass extends Wind {
  void play(Note n) { print("Brass.play() " + n); }
  void adjust() { print("Adjusting Brass"); }
}

class Woodwind extends Wind {
  void play(Note n) { print("Woodwind.play() " + n); }
  public String toString() { return "Woodwind"; }
}	

public class Music3 {
  // Doesn't care about type, so new types
  // added to the system still work right:
  public static void tune(Instrument i) {
    // ...
    i.play(Note.MIDDLE_C);
  }
  public static void tuneAll(Instrument[] e) {
    for(Instrument i : e)
      tune(i);
  }	
  public static void main(String[] args) {
    // Upcasting during addition to the array:
    Instrument[] orchestra = {
      new Wind(),
      new Percussion(),
      new Stringed(),
      new Brass(),
      new Woodwind()
    };
    tuneAll(orchestra);
    for(Instrument i:orchestra)
    	print(i);
  }
} 

7、是的
8、

//: polymorphism/music3/Music3.java
// An extensible program.
package polymorphism.music3;
import polymorphism.music.Note;
import static net.mindview.util.Print.*;
import java.util.*;

class Instrument {
  void play(Note n) { print("Instrument.play() " + n); }
  public String toString() { return "Instrument"; }
  void adjust() { print("Adjusting Instrument"); }
}

class Wind extends Instrument {
  void play(Note n) { print("Wind.play() " + n); }
  public String toString() { return "Wind"; }
  void adjust() { print("Adjusting Wind"); }
}	

class Percussion extends Instrument {
  void play(Note n) { print("Percussion.play() " + n); }
  public String toString() { return "Percussion"; }
  void adjust() { print("Adjusting Percussion"); }
}

class Stringed extends Instrument {
  void play(Note n) { print("Stringed.play() " + n); }
  public String toString() { return "Stringed"; }
  void adjust() { print("Adjusting Stringed"); }
}

class Brass extends Wind {
  void play(Note n) { print("Brass.play() " + n); }
  public String toString() {
	  return "Brass";
  }
  void adjust() { print("Adjusting Brass"); }
}

class Woodwind extends Wind {
  void play(Note n) { print("Woodwind.play() " + n); }
  public String toString() { return "Woodwind"; }
}	

public class Music3 {
  // Doesn't care about type, so new types
  // added to the system still work right:
private Random rand=new Random(47);
  public static void tune(Instrument i) {
    // ...
    i.play(Note.MIDDLE_C);
  }
  public static void tuneAll(Instrument[] e) {
    for(Instrument i : e)
      tune(i);
  }	
  public Instrument next() {
	  switch(rand.nextInt(5)) {
	  default:
	  case 0:return new Wind();
	  case 1:return new Percussion();
	  case 2:return new Stringed();
	  case 3:return new Brass();
	  case 4:return new Woodwind();  
	  }
  }
  public static void main(String[] args) {
    // Upcasting during addition to the array:
	  Music3 m=new Music3();
Instrument[]i2=new Instrument[9];
    for(int i=0;i<i2.length;i++)
    	i2[i]=m.next();
    tuneAll(i2);
    for(Instrument i:i2)
    	print(i);
  }
}

9、

package ch8;
import java.util.*;
class Rodent{
void say() {
	System.out.println("im Rodent");
}
}
class Mouse extends Rodent{
void say() {
	System.out.println("im Mouse");
}
}
class Gerbil extends Rodent{
void say() {
	System.out.println("im Gerbil");
}
}
class Hamster extends Rodent{
void say() {
	System.out.println("im Hamster");
}
}
public class UseRodent {
private Random rand=new Random(47);
Rodent get() {
	switch(rand.nextInt(3)) {
	default:
	case 0:return new Mouse();
	case 1:return new Gerbil();
	case 2:return new Hamster();
	}	
}
public static void main(String[] args) {
	UseRodent ur=new UseRodent();
	Rodent[] r=new Rodent[5];
	for(int i=0;i<r.length;i++)
		r[i]=ur.get();
	for(Rodent i:r)
		i.say();
}
}

10、

package ch8;
class B{
void a() {
b();
}
void b() {
System.out.println("b");
}
}
public class A extends B{
void b() {
System.out.println("A.b");
}
public static void main(String[] args) {
B a2=new A();
a2.a();	
}
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

returnadsss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值