Java编程思想第八章习题答案(2)

11、

package polymorphism;
import static net.mindview.util.Print.*;

class Meal {
  Meal() { print("Meal()"); }
}

class Bread {
  Bread() { print("Bread()"); }
}

class Cheese {
  Cheese() { print("Cheese()"); }
}

class Lettuce {
  Lettuce() { print("Lettuce()"); }
}
class Pickle extends Lettuce{
  Pickle(){
	  print("Pickle()");
  }
}

class Lunch extends Meal {
  Lunch() { print("Lunch()"); }
}

class PortableLunch extends Lunch {
  PortableLunch() { print("PortableLunch()");}
}

public class Sandwich extends PortableLunch {
  private Bread b = new Bread();
  private Cheese c = new Cheese();
  private Lettuce l = new Lettuce();
  public Sandwich() { print("Sandwich()"); }cdx
  public static void main(String[] args) {
    new Sandwich();
    new Pickle();
  }
}

12、

package ch8;
import java.util.*;
class Rodent{
private String r=get("Rodent");
Rodent() {
	System.out.println("im Rodent");
}
String get(String s){
	System.out.println(s);
    return s;
}
}
class Mouse extends Rodent{
private String r=get("Mouse");
Mouse() {
	System.out.println("im Mouse");
}
}
class Gerbil extends Rodent{
Gerbil() {
	System.out.println("im Gerbil");
}
}
class Hamster extends Rodent{
Hamster() {
	System.out.println("im Hamster");
}
}
public class UseRodent extends Mouse{
private String r=get("Mouse");
public static void main(String[] args) {
	UseRodent ur=new UseRodent();
    
}
}

13、

package ch8;
class Shared{
	private int refcount=0;
	private static long counter=0;
	private final long id=counter++;
	public Shared() {
		System.out.println("Creating"+this);
	}
	public void addRef() {
		refcount++;
	}
	protected void dispose() {
		if(--refcount==0)
			System.out.println("Disposing"+this);
	}
	public String toString() {
		return "Shared"+id;
	}
	protected void finalize() {
		if(refcount!=0) {
			System.out.println("you should dispose it");
		}
	}
}
class Composing {
	private Shared shared;
	private static long counter=0;
	private final long id=counter++;
	public Composing(Shared shared) {
		System.out.println("Creating"+this);
		this.shared=shared;
		this.shared.addRef();
	}
	protected void dispose() {
		System.out.println("disposing"+this);
		shared.dispose();
	}
	public String toString() {
		return "Composing"+id;
	}
}	
public class ReferenceCounting {
public static void main(String[] args) {
	Shared shared=new Shared();
	Composing[] composing= {
			new Composing(shared),new Composing(shared),new Composing(shared),new Composing(shared),
			new Composing(shared)
	};
	for(int i=0;i<composing.length-1;i++)
		composing[i].dispose();
	shared.finalize();
}
}

14、

package ch8;
import java.util.*;
class Rodent{
//private String r=get("Rodent");
Rodent() {
	//System.out.println("im Rodent");
}
String get(String s){
	//System.out.println(s);
    return s;
}
}
class Mouse extends Rodent{
private String r=get("Mouse");
private static long id=0;
Mouse() {
	id++;
	System.out.println("i "+this);
}
public String toString() {
	return "Mouse"+id;
}
}

/*
 * class Gerbil extends Rodent{ Gerbil() { System.out.println("im Gerbil"); } }
 * class Hamster extends Rodent{ Hamster() { System.out.println("im Hamster"); }
 * }
 */
public class UseRodent extends Mouse{
public static void main(String[] args) {
	Mouse[] m= {new Mouse(),new Mouse(),new Mouse()};
    
}
}

15、

package ch8;
class Glyph{
	void draw() {
		System.out.println("Glyph.draw()");
	}
	Glyph(){
		System.out.println("Glyph() before draw()");
		draw();
		System.out.println("Glyph() after draw()");
	}
}
class RectangleGlyph extends Glyph{
	private int ra=1;
	RectangleGlyph(int r){
		System.out.println("RectangleGlyph(),ra="+ra);
	}
	void draw() {
		System.out.println("RectangleGlyph(),ra="+ra);
	}
}
class RoundGlyph extends Glyph{
	private int radius=1;
	RoundGlyph(int r){
		radius=r;
		System.out.println("RoundGlyph.RoundGlyph(),radius="+radius);
	}
	void draw() {
		System.out.println("RoundGlyph.draw(),radius="+radius);
	}
}
public class PolyConstructors {
public static void main(String[] args) {
	new RectangleGlyph(5);
}
}

16、

package ch8;
class Status{
	public void show() {}
}
class Status1 extends Status{
	public void show() {
		System.out.println("Status1");
	}
}
class Status2 extends Status{
	public void show() {
		System.out.println("Status2");
	}
}
class Status3 extends Status{
	public void show() {
		System.out.println("Status3");
	}
}
class AlertStatus{
	private Status s=new Status1();
	void alert(){
		s.show();
	}
	void change1() {
		s=new Status2();
	}
	void change2() {
		s=new Status3();
	}
}
public class Starship {
public static void main(String[] args) {
	AlertStatus as=new AlertStatus();
	as.alert();
	as.change1();
	as.alert();
	as.change2();
	as.alert();
}
}

17、

package ch8;
class Cycle{
void balance() {};
}
class Unicycle extends Cycle{
void balance() {
	
}
}
class Bicycle extends Cycle{
void balance() {
	
}
}
class Tricycle extends Cycle{

}
public class UseCycle {
public static void main(String[] args) {
Cycle[] c= {new Unicycle(),new Bicycle(),new Tricycle()};
c[0].balance();
c[1].balance();
c[2].balance();
((Unicycle)c[0]).balance();
((Bicycle)c[1]).balance();
((Tricycle)c[2]).balance();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

returnadsss

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

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

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

打赏作者

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

抵扣说明:

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

余额充值