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