I think you need to read a good book.SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055) by Kathy Sierra and Bert Bates McGraw-Hill/Osborne 2006 (864 pages) ISBN:0072253606
刚给LZ写的 AbsDoor.java //门的抽象类 Door.java //门,也是运行类 IRing.java //门铃的接口 Ring.java //门铃 Sounds.java //音乐 /** * @author infon * 2007-5-17 */ public class Door extends AbsDoor { public Door(){ height=2.2f; width=1.2f; ring=new Ring(); } /* * 重写 * (non-Javadoc) * @see AbsDoor#open() */ @Override void open() { // TODO Auto-generated method stub System.out.println("主人听到门铃,去开门"); System.out.println("开门"); } /** * @param args */ public static void main(String[] args) { System.out.println("按门铃不说话的演示:"); Door door1=new Door(); door1.ring.press(); Sounds sounds1=door1.ring.notice(); sounds1.sing(); door1.open(); System.out.println(); System.out.println("按门铃大喊的演示:"); Door door2=new Door(); door2.ring.press("我是小梅,找你有急事,快点来开门"); Sounds sounds2=door2.ring.notice(); sounds2.sing(); door2.open(); } }/** * @author infon * 2007-5-17 */ public abstract class AbsDoor { float height; float width; IRing ring; abstract void open(); } public interface IRing { //敲门时不出声 void press(); //重载 //有人按门铃,然后在门铃上喊,门铃能把声音传进屋子里 void press(String calling); //传送声音至主人 Sounds notice(); } public class Ring implements IRing {
Sounds sounds; public Sounds notice() { // TODO Auto-generated method stub return sounds; } /** * 重写 */ public void press() { // TODO Auto-generated method stub System.out.println("客人敲门"); sounds=new Sounds(); }
/** * 重写 * @param calling */ public void press(String calling) { // TODO Auto-generated method stub System.out.println("敲门"); sounds=new Sounds(calling); } }import java.util.Random;public class Sounds { String words[] = new String[] { "主人,有客来了,请开门", "美女来了,快开门呀", "小兔子乖乖,把门儿开开", "开门,开门,快点来开门", "快点把门打开吧,外面好冷" }; String word; public Sounds() { Random rand = new Random(); int index = Math.abs(rand.nextInt()) % 5; word=words[index]; }
public Sounds(String word) { this.word=word; } /** * 打印铃声 */ public void sing() { System.out.println("门铃响:"+word); } }
package exam;public abstract class AbstactAnimal implements IAnimal { private int iLegCount; public abstract void feed(); public abstract void feed(int iHowmuch);
public void walk() { System.out.println(this.getSName() + " is walking."); } public abstract int getILegCount() ; } package exam;public class people extends AbstactAnimal { private boolean isClever; private String sName; public people(boolean isClever,String sName) { this.isClever = isClever; this.sName = sName; } @Override public void feed() { if(this.isClever) { new CleverMan(this.getSName()).feed(); } else { System.out.println(this.getSName() + " is feeding."); } }
@Override public void walk() { if(this.isClever) { new CleverMan(this.getSName()).walk(); } else { super.walk(); } } @Override public void feed(int iHowmuch) { if(this.isClever) { new CleverMan(this.getSName()).feed(iHowmuch); } else { System.out.println(this.getSName() + " is feeding with (" + iHowmuch + ")."); } }
private class CleverMan extends people { public CleverMan(String sName) { super(true,sName); // TODO Auto-generated constructor stub }
@Override public void feed() { System.out.println(this.getSName() + " is feeding fast."); } @Override public void feed(int iHowmuch) { System.out.println(this.getSName() + " is feeding fast with (" + iHowmuch + ")."); }
@Override public void walk() { System.out.println(this.getSName() + " is running."); }
} @Override public int getILegCount() { // TODO Auto-generated method stub return 2; } public String getSName() { return sName; } public void setSName(String name) { sName = name; }}/** * */ package exam;/** * @author KimmKing * */ public class test { /** * @param args */ public static void main(String[] args) { IAnimal ia1 = new people(false,"jpweng"); ia1.feed(); ia1.walk();
IAnimal ia2 = new people(true,"Kimm King"); ia2.feed(); ia2.walk(); }}
by Kathy Sierra and Bert Bates
McGraw-Hill/Osborne 2006 (864 pages)
ISBN:0072253606
AbsDoor.java //门的抽象类
Door.java //门,也是运行类
IRing.java //门铃的接口
Ring.java //门铃
Sounds.java //音乐
/**
* @author infon
* 2007-5-17
*/
public class Door extends AbsDoor {
public Door(){
height=2.2f;
width=1.2f;
ring=new Ring();
}
/*
* 重写
* (non-Javadoc)
* @see AbsDoor#open()
*/
@Override
void open() {
// TODO Auto-generated method stub
System.out.println("主人听到门铃,去开门");
System.out.println("开门");
} /**
* @param args
*/
public static void main(String[] args) {
System.out.println("按门铃不说话的演示:");
Door door1=new Door();
door1.ring.press();
Sounds sounds1=door1.ring.notice();
sounds1.sing();
door1.open();
System.out.println();
System.out.println("按门铃大喊的演示:");
Door door2=new Door();
door2.ring.press("我是小梅,找你有急事,快点来开门");
Sounds sounds2=door2.ring.notice();
sounds2.sing();
door2.open();
}
}/**
* @author infon
* 2007-5-17
*/
public abstract class AbsDoor {
float height;
float width;
IRing ring;
abstract void open();
}
public interface IRing {
//敲门时不出声
void press();
//重载
//有人按门铃,然后在门铃上喊,门铃能把声音传进屋子里
void press(String calling);
//传送声音至主人
Sounds notice();
}
public class Ring implements IRing {
Sounds sounds; public Sounds notice() {
// TODO Auto-generated method stub
return sounds;
} /**
* 重写
*/
public void press() {
// TODO Auto-generated method stub
System.out.println("客人敲门");
sounds=new Sounds();
}
/**
* 重写
* @param calling
*/
public void press(String calling) {
// TODO Auto-generated method stub
System.out.println("敲门");
sounds=new Sounds(calling);
}
}import java.util.Random;public class Sounds {
String words[] = new String[] { "主人,有客来了,请开门", "美女来了,快开门呀", "小兔子乖乖,把门儿开开",
"开门,开门,快点来开门", "快点把门打开吧,外面好冷" };
String word;
public Sounds() {
Random rand = new Random();
int index = Math.abs(rand.nextInt()) % 5;
word=words[index];
}
public Sounds(String word) {
this.word=word;
}
/**
* 打印铃声
*/
public void sing() {
System.out.println("门铃响:"+word);
}
}
void feed();
void walk();
String getSName();
void setSName(String name);
}
public abstract void feed(int iHowmuch);
public void walk() { System.out.println(this.getSName() + " is walking."); } public abstract int getILegCount() ;
}
package exam;public class people extends AbstactAnimal { private boolean isClever;
private String sName;
public people(boolean isClever,String sName) {
this.isClever = isClever;
this.sName = sName;
} @Override
public void feed() {
if(this.isClever)
{
new CleverMan(this.getSName()).feed();
}
else
{
System.out.println(this.getSName() + " is feeding.");
}
}
@Override
public void walk() {
if(this.isClever)
{
new CleverMan(this.getSName()).walk();
}
else
{
super.walk();
}
} @Override
public void feed(int iHowmuch) { if(this.isClever)
{
new CleverMan(this.getSName()).feed(iHowmuch);
}
else
{
System.out.println(this.getSName() + " is feeding with (" + iHowmuch + ").");
}
}
private class CleverMan extends people
{
public CleverMan(String sName) {
super(true,sName);
// TODO Auto-generated constructor stub
}
@Override
public void feed() {
System.out.println(this.getSName() + " is feeding fast.");
} @Override
public void feed(int iHowmuch) {
System.out.println(this.getSName() + " is feeding fast with (" + iHowmuch + ").");
}
@Override
public void walk()
{
System.out.println(this.getSName() + " is running.");
}
} @Override
public int getILegCount() {
// TODO Auto-generated method stub
return 2;
} public String getSName() {
return sName;
} public void setSName(String name) {
sName = name;
}}/**
*
*/
package exam;/**
* @author KimmKing
*
*/
public class test { /**
* @param args
*/
public static void main(String[] args) { IAnimal ia1 = new people(false,"jpweng");
ia1.feed();
ia1.walk();
IAnimal ia2 = new people(true,"Kimm King");
ia2.feed();
ia2.walk(); }}