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);
}
}
//接口非常近似于类,而且和抽象类更加相近。