62,614
社区成员
发帖
与我相关
我的任务
分享
long Fibonacci(int n)
{
long coef1=1,coef2=1,coef_tmp=0;
int j=0;
if(n <3)
return 1;
j=n-1;
while(j>2)
{
coef_tmp=coef1;
coef1=coef1+coef2;
coef2=coef_tmp;
j--;
}
return coef1+coef2;
}
class Dog{
private String dName;
//拥有多个监听者的引用,由狗本身来提醒他们
private List<ActionListener> listeners = new ArrayList<ActionListener>();
//事件源
private Event e = new Event(this);
//狗叫的行为
public void jiao(){
System.out.println(dName+"叫了!!");
for(ActionListener l : listeners){
l.actionPerformed(e);
}
}
public void addListener(ActionListener listener){
if(listener != null){listeners.add(listener);}
}
public String getDName() {
return dName;
}
public void setDName(String name) {
dName = name;
}
}
//监听器
interface ActionListener{
public void actionPerformed(Event e);
}
//事件源
class Event{
Object obj = null;
public Event(Object obj){
this.obj = obj;
}
}
class Master implements ActionListener{
public void actionPerformed(Event e) {
System.out.println("主人醒了!!");
}
}
class Thief implements ActionListener{
public void actionPerformed(Event e) {
System.out.println("小偷吓跑了!!");
}
}
public class Test {
public static void main(String[] args) {
Master m = new Master();
Thief t = new Thief();
Dog d = new Dog();
d.setDName("牧羊狗!!");
d.addListener(m);
d.addListener(t);
d.jiao();
}
}