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();
}
}