连动反应
来源:互联网 发布:篮球比赛计分软件 编辑:程序博客网 时间:2024/04/27 14:57
猫叫 老鼠叫 主人醒
#include <iostream>
#include <string>
#include <stdlib.h>
#include <vector>
using namespace std;
class Animal
{
public:
virtual string Getname()=0;
virtual void Respond(Animal * animal,int value)=0;
};
class Mouse:public Animal
{
public:
string Name;
Mouse(string name)
{
Name=name;
}
string Getname()
{
return Name;
}
void Respond(Animal *animal,int value)
{
if(value>2000)
{
cout<<"Mouse("<<Name<<"):"<<animal->Getname()<<" is coming!"<<endl;
}
}
};
class Human:public Animal
{
public:
string Name;
Human(string name)
{
Name=name;
}
string Getname()
{
return Name;
}
void Respond(Animal*animal,int value)
{
if(value>100)
{
cout<<"Human("<<Name<<"):"<<animal->Getname()<<" is anoyying!"<<endl;
}
}
};
class Cat:public Animal
{
public:
string Name;
vector <Animal*> response;
Cat(string name)
{
Name=name;
}
string Getname()
{
return Name;
}
void Respond(Animal *animal,int value)
{
if(value>50)
{
cout<<"cat("<<Name<<"):"<<animal->Getname()<<" is there!"<<endl;
}
}
void Add(Animal *animal)
{
response.push_back(animal);
}
void cry()
{
int value=rand();
cout<<value<<endl;
vector<Animal *>::iterator it;
for(it=response.begin();it!=response.end();it++)
{
Animal *animal=*it;
animal->Respond(this,value);
}
}
};
int main()
{
Mouse m1("a"),m2("b");
Human h1("c"),h2("d"),h3("e");
Cat cat("tom");
cat.Add(&m1);
cat.Add(&m2);
cat.Add(&h1);
cat.Add(&h2);
cat.Add(&h3);
for(int i=0;i<10;i++)
{
cat.cry();
}
return 0;
}
天下雨 汽车开始减速 行人开始奔跑 小贩开始收摊
#include <iostream>
#include <vector>
#include <stdlib.h>
using namespace std;
class A
{
public:
// int value;
virtual void Respond(A * p,int value)=0;
};
class Car:public A
{
public:
int value;
void Respond (A*p,int value)
{
if(value>10000&&value<20000)
{
cout<<"rain!!! cars begin to slow down!"<<endl;
}
else
{
cout<<"The sky is not rainning! Cars continue go at the current speed!"<<endl;
}
}
};
class Footman:public A
{
public:
// int value;
void Respond(A*p ,int value)
{
if(value>10000&&value<20000)
{
cout<<"rain!!! Footmen begin to run!"<<endl;
}
else
{
cout<<"The sky is not rainning! Footmen continue to walk!"<<endl;
}
}
};
class Trader:public A
{
public:
// int value;
void Respond(A*P,int value)
{
if(value>10000&&value<20000)
{
cout<<"rain!!! Traders stop trading!"<<endl;
}
else
{
cout<<"The sky is not rainning! Traders continue trading!"<<endl;
}
}
};
class Sky:public A
{
public:
// int value;
vector<A*> R;
void Respond(A*p,int value)
{
if(value>10000&&value<20000)
{
cout<<"The sky is rainning!"<<endl;
}
}
void Add(A*p)
{
R.push_back(p);
}
void Rain()
{
int value=rand();
cout<<value<<endl;
vector<A*>::iterator it;
for(it=R.begin();it!=R.end();it++)
{
A* p=*it;
p->Respond(this,value);
}
}
};
int main()
{
Car car;
Footman footman;
Trader trader;
Sky sky;
sky.Add(&car);
sky.Add(&footman);
sky.Add(&trader);
for(int i=0;i<10;i++)
{
sky.Rain();
}
return 0;
}
- 连动反应
- 连动反应——猫叫 老鼠跑 主人醒
- 连动
- 日期连动
- DropDownList连动菜单
- 省市连动
- XMLHTTP二级连动
- 多级的连动
- 二级连动
- 省市三级连动
- DynamicOptionList连动菜单
- 实现省市连动
- jsp连动菜单
- 二级连动
- DropDownList实现连动
- ext ComboBox 连动
- 图片连动
- ecshop区域连动
- Eclipse开发过程中个VM Arguments的设置
- 一个简单的聊天室程序
- 让青春飞扬
- 动态调度
- 解决用easyboot制作4G以上光盘时,WINPE启动失败的问题
- 连动反应
- Android多种数据库存储方式对比
- 开博。。。
- 完成联网版的坦克程序心得
- webservice---axis2--part----1
- 不要制造不必要的混乱把优化器给弄糊涂了
- asm goto与JUMP_LABEL
- 2011年
- JSF2自定义组件编程系列 第七部分