Java编程思想第九章习题(1)

1、

package ch8;
import java.util.*;
abstract class Rodent{
	abstract void shout();
}
class Mouse extends Rodent{
void shout() {
	System.out.println("Mouse");
}
}
public class UseRodent {
public static void main(String[] args) {
  Mouse m=new Mouse();
  m.shout();
}
}

2、

package ch8;
import java.util.*;
abstract class Rodent{
}
class Mouse extends Rodent{
}
public class UseRodent {
public static void main(String[] args) {
	//Rodent r=new Rodent(); Error!
  Mouse m=new Mouse();
}
}

3、

package ch8;
import java.util.*;
abstract class Rodent{
Rodent(){
print();
}
abstract void print();
}
class Mouse extends Rodent{
int a=4;
void print() {
	System.out.println(a);
}
}
public class UseRodent {
public static void main(String[] args) {
Mouse m=new Mouse();
m.print();
}
}

4、

package ch8;
import java.util.*;
class Rodent{
}
class Mouse extends Rodent{
void say() {
	System.out.println("Hi");
}
}
abstract class Rodent2{
abstract void say2();
}
class Mouse2 extends Rodent2{
void say2() {
	System.out.println("Hi2");
}
}
public class UseRodent {
static void get(Rodent t) {
	((Mouse)t).say();
}
static void get2(Rodent2 t) {
	t.say2();
}
public static void main(String[] args) {
Rodent r=new Mouse();
get(r);
Rodent2 r2=new Mouse2();
get2(r2);
}
}

5、

package ch9;

public interface Color {
void red();
void yellow();
void green();
}

package ch8;
import ch9.Color;
public class UseColor implements Color{
public void red() {
	System.out.println("red");
}
public void green() {
	System.out.println("green");
}
public void yellow() {
	System.out.println("yellow");
}
public static void main(String[] args) {
	UseColor us=new UseColor();
	us.red();
	us.green();
	us.yellow();
}
}

6、第5题
7、

package ch9;
import java.util.*;
interface Rodent{
void say();
}
class Mouse implements Rodent{
public void say() {
	System.out.println("im Mouse");
}
}
class Gerbil implements Rodent{
public void say() {
	System.out.println("im Gerbil");
}
}
class Hamster implements Rodent{
public 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();
}
}

8、

package ch9;

class Meal {
}

class Lunch extends Meal {
}

class PortableLunch extends Lunch {
}
interface FastFood{
void eat();
}
public class Sandwich extends PortableLunch implements FastFood{
public void eat() {
	System.out.println("eat quickly");
}
public static void main(String[] args) {
 Sandwich s=new Sandwich();
 s.eat();
}
}

9、

package ch9;
import polymorphism.music.Note;
import static net.mindview.util.Print.*;

abstract class Instrument {
  // Compile-time constant:
  int VALUE = 5; // static & final
  // Cannot have method definitions:
  void play(Note n) {
	  print("Instrument.play"+n);
  }; // Automatically public
  abstract void adjust();
}

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

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

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

class Brass extends Wind {
  public String toString() { return "Brass"; }
}	

class Woodwind extends Wind {
  public String toString() { return "Woodwind"; }
}

public class Music5 {
  // Doesn't care about type, so new types
  // added to the system still work right:
  static void tune(Instrument i) {
    // ...
    i.play(Note.MIDDLE_C);
  }
  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);
  }
} 

10、

package ch9;
import polymorphism.music.Note;
import static net.mindview.util.Print.*;

abstract class Instrument {
  // Compile-time constant:
  int VALUE = 5; // static & final
  // Cannot have method definitions:
  abstract void adjust();
}
interface Playable{
  void play(Note n);
}
class Wind extends Instrument implements Playable{
  public void play(Note n) {
    print(this + ".play() " + n);
  }
  public String toString() { return "Wind"; }
  public void adjust() { print(this + ".adjust()"); }
}

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

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

class Brass extends Wind {
  public String toString() { return "Brass"; }
}	

class Woodwind extends Wind {
  public String toString() { return "Woodwind"; }
}

public class Music5 {
  // Doesn't care about type, so new types
  // added to the system still work right:
  static void tune(Playable i) {
    // ...
    i.play(Note.MIDDLE_C);
  }
  static void tuneAll(Playable[] e) {
    for(Playable i : e)
      tune(i);
  }	
  public static void main(String[] args) {
    // Upcasting during addition to the array:
	  Playable[] orchestra = {
      new Wind(),
      new Percussion(),
      new Stringed(),
      new Brass(),
      new Woodwind()
    };
    tuneAll(orchestra);
  }
} 
//接口非常近似于类,而且和抽象类更加相近。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

returnadsss

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

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

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

打赏作者

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

抵扣说明:

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

余额充值