当前位置: 代码迷 >> J2SE >> 跪求简单java程序源码解决方案
  详细解决方案

跪求简单java程序源码解决方案

热度:184   发布时间:2016-04-24 15:28:15.0
跪求简单java程序源码
求一个简单的java代码,要求使用到继承,多态性,抽象类,接口还有内部类。什么都行,不能用全的话尽量使用也行,拜托了,救小弟一命!!!!!

------解决方案--------------------
刚给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 interface IAnimal {

void feed();
void walk();
String getSName();
void setSName(String name);
}
  相关解决方案